Variables in JavaScript – Dev Community

In JavaScript, variables are containers in which we can store some information.

There are 3 ways declared Variables:

  1. var(used 1995-2015);
  2. Come on (created in 2015);
  3. const (created in 2015).

The differences between these three types will be discussed later.

The declaration of a variable begins with the variable type, and then we write the name of the variable we want to create;

                       let name;
enter fullscreen mode

exit fullscreen mode

There are several rules for naming variables in JS:

  • We can use letters, numbers, underscore (_), and dollar symbol ($);

  • The name must begin with a letter; _ or $;

  • Variable names are case sensitive, which means that lowercase (y) and uppercase (Y) are different;

  • We cannot use reserved names, such as JS keywords or variable names.

After declaring variables, we can assign some values ​​to these variables. In JS, it is called tell me, We assign a value to the variable using the = symbol:

                   let name;
                   name = value;
enter fullscreen mode

exit fullscreen mode

Alternatively, we assign the value immediately after creating the variable:

                let name = value; 
enter fullscreen mode

exit fullscreen mode

The value can be:

  • number,

                let name = 42; 
    
  • a text called string js, and written in double or single quotation marks (” or “”):

                let name = "This is a string"; 
    
  • One boolean valuewhich means the variable is ether Truth either false,

                let name = true;
    

There are some differences between var, let, and const.
There is a difference whether it may or may not re-announced, const and let can only be declared in a single {} or globally, while var can be re-declared as many times as desired.
So

                  var x = 10;
                  var x = 25;
enter fullscreen mode

exit fullscreen mode

going to work, but

                  let x = 10;
                  let x = 25;
enter fullscreen mode

exit fullscreen mode

And

                  const x = 10;
                  const x = 25;
enter fullscreen mode

exit fullscreen mode

Going to result in an error.
Nevertheless, const and let can be re-declared in {} separately.

Another difference is whether the variables can be used before declaring, For var , this will work, whereas for let and const , they must be declared before they can be used:
So

                     x = x + 7;
                     var x; 
enter fullscreen mode

exit fullscreen mode

while will work

                     x = x + 7;
                     let x; 
enter fullscreen mode

exit fullscreen mode

And

                     x = x + 7;
                     const x; 
enter fullscreen mode

exit fullscreen mode

will result in an error.

Another difference between variable declaration types is their scope, The scope determines the **visibility** or **accessibility** of the variables.

Let, and the constants are block ** scope, whereas var is not. This means that when a variable is created inside {} with let or const , it is local, and it does not work outside {} , but if the variable is created by var , it is called * *Manufactured globally, Outside {} , when variables are created, they are the same because they all have global scope.

So far we haven’t seen any difference between let and const . Let can be continuously reassigned with a new value, whereas const is constant and does not change its value.
So

             const PI = 3.141592653589793;
             PI = 3.14;  
enter fullscreen mode

exit fullscreen mode

will give an error, but

             let number = 93;
             number = 444;
enter fullscreen mode

exit fullscreen mode

Will reassign 444 to the variable number.
However, the term constant is a bit misleading. To a static variable, we cannot reassign a constant value, array or object, but we can change the array’s elements and the object’s value properties.

Leave a Comment