Using variables and constants wherever possible and appropriate is good coding practice. It is also good coding practice to declare your variables and constants.

1. Some code

This tutorial starts with the code we wrote in the “Hello World!” in JavaScript tutorial:

<html>
    <head>
        <title>Hello World! JavaScript</title>
    </head>
    <body>
        <h1>Hello World! JavaScript</h1>
        <script>
            alert("Hello World!");
        </script>
    </body>
</html>

There is nothing wrong with this code; it works.

2. undeclared

The next step would be to assign a value to a variable and then use the variable, like so:

<html>
    <head>
        <title>Hello World! JavaScript</title>
    </head>
    <body>
        <h1>Hello World! JavaScript</h1>
        <script>
                myGreeting = 'Hello World!';
                alert(myGreeting);
        </script>
    </body>
</html>

3. var

You will see var used in a lot of code. It should only be used in code for much older browsers.

It is good practice to declare your variable type. Here we declare the variable and assign a string to it in one go:

<html>
    <head>
        <title>Hello World! JavaScript</title>
    </head>
    <body>
        <h1>Hello World! JavaScript</h1>
        <script>
                var myGreeting = 'Hello World!';
                alert(myGreeting);
        </script>
    </body>
</html>

4. let

It is preferable to use let in place of var:

<html>
    <head>
        <title>Hello World! JavaScript</title>
    </head>
    <body>
        <h1>Hello World! JavaScript</h1>
        <script>
                let myGreeting = 'Hello World!';
                alert(myGreeting);
        </script>
    </body>
</html>

5. constant

In this example, the greeting is always the same. It does not change — and we don’t want it to change — during the running of the program. Here we use a constant:

<html>
    <head>
        <title>Hello World! JavaScript</title>
    </head>
    <body>
        <h1>Hello World! JavaScript</h1>
        <script>
                const myGreeting = 'Hello World!';
                alert(myGreeting);
        </script>
    </body>
</html>

6. When to use which?

I cannot state this better than the gurus at W3Schools.com :

When to use var, let, or const?

  1. Always declare variables
  2. Always use const if the value should not be changed
  3. Always use const if the type should not be changed (Arrays and Objects)
  4. Only use let if you can’t use const
  5. Only use var if you MUST support old browsers. [1]

The var and let keywords are not exactly the same. They specifically differ in terms of scope. [2]


References:

  1. W3Schools Online Web Tutorials, W3Schools.com (no date) JavaScript Variables. Available at: https://www.w3schools.com/js/js_variables.asp (Accessed: 17 October 2024).
  2. , W3Schools Online Web Tutorials, W3Schools.com (no date) JavaScript Let. Available at: https://www.w3schools.com/js/js_let.asp (Accessed: 17 October 2024).

By MisterFoxOnline

Mister Fox AKA @MisterFoxOnline is an ICT, IT and CAT Teacher who has just finished training as a Young Engineers instructor. He has a passion for technology and loves to find solutions to problems using the skills he has learned in the course of his IT career.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.