DEV Community


What to choose VAR, LET or CONST?

ayushmann profile image Ayushman Parasar ・3 min read

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.


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
  3. It allocates space for a and assigns it to the value of undefined.
  4. 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,

Discussion (0)

Editor guide