QP School

Full Version: Exercise 0003 - Variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Variables are some kind of drawers with captions.
The captions are a hint for what kind of stuff can be found inside the drawers.
In the case of variables it can be for example numbers or text.

The older way to declare variables in JavaScript is using the var keyword.
The newer way to declare variables in JavaScript is using the let keyword.
We will use the newer way here.

Code:
let txt = "Hello, World!"

In the first example we will declare a variable, assign a value to it and show the output in a HTML document:

Code:
<!DOCTYPE HTML>

<html>

<head>
    <title>Variables</title>
</head>

<body>

    <script>
        let x = 100;
        document.write(x);
    </script>

</body>

</html>

In the second example we will change the value of the variable and show the output before and after the change in a HTML document:

Code:
<!DOCTYPE HTML>

<html>

<head>
  <title>Untitled</title>
</head>

<body>

    <script>
        var x = 100;
        document.write(x);
    </script>
    <br>
    <script>
        x = 42;
        document.write(x);
    </script>

</body>

</html>

I've put it in two script tags and added a <br> tag between them, because there might be unexpected results otherwise.