Poll: Was this article helpful for you?
You do not have permission to vote in this poll.
Yes
100.00%
1 100.00%
No
0%
0 0%
Total 1 vote(s) 100%
* You voted for this item. [Show Results]

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exercise 0003 - Variables
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Exercise 0002 - Hello World in JavaScript in the browser console Qomplainerz 0 954 04-10-2022, 07:45 AM
Last Post: Qomplainerz
  Exercise 0001 - Hello World in JavaScript within a HTML document Qomplainerz 0 913 04-10-2022, 07:43 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 6 Guest(s)