QP School
Exercise 0003 - Variables - Printable Version

+- QP School (https://qomplainerzschool.lima-city.de)
+-- Forum: Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=3)
+--- Forum: JavaScript Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=6)
+--- Thread: Exercise 0003 - Variables (/showthread.php?tid=3361)



Exercise 0003 - Variables - Qomplainerz - 04-10-2022

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.