JavaScript Variables


Published on 25 March 2022


JavaScript Variables

 

A variable is a name that is assigned a value that can be used at any time within our javascript code. 
It starts with the reserved word "var", "let" or "const" followed by our variable name, equal to the value we want to give it between quotes. Always ending with a ";".
For example:

var text = "this is a message";

To use this message you simply have to write the name of the variable at the time you want. 
That is, in this case, you simply have to write "text" at the time you want.

In the following example we declare the numeric variable var_num, assign it a value and declare it inside console.log to display its content inside the browser console.

const name_var = 18;
console.log(name_var);

Variables can be of type:

  • Global. Defined variables work within all of our javascript code.
  • Local. Defined variables only work in some parts of our javascript code.

Generally our variables have to be defined at the beginning of our code, so that later they can be used at the time we want.

We define the variable types as follows:

  • Variable type var. Designated as "var" followed by the variable name. Its value can be changed at any time throughout our javascript code. It is a type of variable that saves us from possible errors in javascript, since if it is not defined, the code will never return an error, it will only indicate that this variable has no value.
  • Type of let variable. Designated as "let" followed by the variable name. Its value can be changed at any time throughout our javascript code. This type of variable, unlike the type of variable var, always has to have a defined value, since if it does not have a value, javascript will return an error.
  • Variable type const. Designated as "const" followed by the name of the variable. This variable is assigned a value that does not change throughout our javascript code. This type of variable, like the type of let variable, always has to have a defined value, since if it does not have a value, javascript will return an error.

Code example with variables without errors:

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>TODO write content</div>
        <script type="text/javascript">
            console.log(text2);
            
            const name = "myName";
            let text = 'myLetText';
            var text2 = 'myVarText';

            console.log(name);
            console.log(text);
            console.log(text2);
        </script>
    </body>
</html>

The definition of the name of the variables in javascript is always done as follows:

  • In the event that we want to put a variable name with two words, we must put the two words together, differentiating the first word in lowercase followed by the second word starting in uppercase.
    let nameVariable = 'this is my var';
  • In the event that we want to put a variable name with a single word, we must put the word in lowercase.
    let name = 'this is my var';

Previous Chapter - Next Chapter


Tips on SEO and Online Business

Next Articles

Previous Articles