What to choose VAR, LET or CONST?
Tags: beginners, javascript
Published: 2019-12-19 14:43:01

When we write a program in javascript we use a lot of variables. These variables play an important role as they are name storage of data. So whenever we are declaring a variable we need to do it correctly so as to write error-free code. We can declare the variables using _var_ , _const_ or _let_ .The behavior of let and const is somewhat similar whereas for _var_ the scenario changes. Earlier there was no _let_ and _const_  , but only _var_ . With ES6 both _let_ and _const_ came into action which was a relief for the developers.

__So let us SEE HOW THEY ARE DIFFERENT__

__In terms of assigning:__
```
let a = 4;
let a = 5;//wrong
```

Once I have done this let a = 4;
I cannot do let a = 5 within the same scope now the question is why?

When we are using _let_ the following things happen in the above code:
1. it creates a space for ‘a’,
2. it assigns the value of 4 to a,
Now when we try let a = 5 there is a syntax error that the identifier _a_ has already been declared, this is because it tries to create a second space for the same variable _a_ which is not possible with the use of _let_.
_const_ behaves in a similar way but the  difference is that we need to assign a value at the time of declaration of the variable. That being said it should be _const a = 5_ and not _const a;_.The value of a variable declared with _const_ cannot be updated or re-declared that is it remains the same within its scope
Now let us come to _var_, when we write _var a = 5_
1. It allocates space for _a_ and assigns it to the value of undefined.
2. It assigns the value 5 to _a_
If we reassign _var a = 6_ then there would be no errors this time. This is because at the time when javascript was made it was never made to be at the scale where it is now, so many things were not planned and as such is the case of _var_ .
There is one more thing I got to know is that when we declare variables using _var_ outside of function it becomes a global variable and becomes accessible to the window object which can cause issues in the program but this is not the case with let as it does not attach the variable with the window object.

__In terms of scope:__

First, let us understand what is meant by scope, we can take ‘scope’ to be the area of accessibility. In terms of scope, the variables declared with _var_ are globally accessible or visible whereas the variables that are declared using _let_ are locally accessible or visible within that block. For example
```

    for(let i = 0; i < 5; i++) {
    setTimeout(() => console.log(i))


```

In the code above the value of _i_ will be 0 then 1 then and so on till 4 , thus the value of _i_ is constrained to the particular iteration. Now let us see what happens in the case of _var_

```

    for(var i = 0; i < 5; i++) {
    setTimeout(() => console.log(i))


```
This time the output will be 5 times 5, what happened is that the loop gets executed and the current value gets stored in the global variable i since setTimeout puts a delay to the function console.log(), for every iteration the current value of _i_ which is 5 is printed.
There are still other things that differentiate these variable declarations but I wanted to keep it at a novice level. I hope I was able to keep things simple for everyone to understand especially for anyone who has just started learning javascript.
So until next time,
Ciao!!!!!!