Difference Between var, let and const in JavaScript

Sourabhkumar
3 min readApr 17, 2021

you have been probably told to use ‘let’ instead of ‘var’ every single time and would have been told the reason for that. But, you forget that. let’s us revise the reason for this and learn about the behavior of var and let.

As you know var are used to declare variables in JS so does the let. The main difference between them is the scope they work in.

variables declared with var keyword are globally scoped whereas variables declared with let keyword have block scope. Have a look at the code:

As x is declared outside any block , it will be accessible through out the code whereas the y is declared inside a block and should not be accessible outside that if block, but surprisingly running this code won’t give you an error. Even though, y is declared inside a block, it is accessible outside that block. This is because variable declared with var keyword are globally-scoped. But, there is a catch to a it, var are also functionally-scoped, let’s understand what is the meaning of functionally-scoped.

As discussed above var variables are globally scoped, therefore, as y is declared inside a function, which has its own scope but y being var will be globally accessed therefore there is no error in this code. But, we will get an error that y is not defined. The var variables are functionally- scoped therefore if declared inside the function will not be accessible outside that function. If we talk in more technical terms var variables are scoped to its execution context and closure.

Now, lets talk about let.

let variables have scope inside the block there are declared and cannot be accessed outside the block, if declared globally will be available globally. They work similar to the variables in strongly typed languages like Java or C++.

As you know functions in JavaScript have their own execution context, hence re declaring let variables inside do not cause problems. But re declaring let variables in same context will cause an error.

No problem in above code, prints 1020.

Now, let us understand why we are told to use let instead of var.

re declaration of var variables in same execution context is allowed in JS.Now, this can cause real troubles in your code, let’s see

What do you think will be the output of code? 20 followed by 10. No, it actually prints 20 followed by 20. Now, this type of code can really cause ambiguity with data and can take days to figure out if code base is large.

Therefore, always follow the golden advice of using let instead of var.

Now, let’s talk about our innocent child const. As it is self- explanatory that variables declared const are used to store fixed value that cannot be changed throughout the code. Not, much to discuss about const but here I present to an example.

Do you think this will cause an error ? as arr is declared constant therefore should not altered and therefore will cause an error, surprisingly it does not. JS allows the modification in the array declared constant. what you cannot do is reassignment of the array:

Thank you….

--

--