Different between var , let and const in Javascript

JS Logo

Before the release of ES6, Javascript only had ‘var’ to declare variables. But with ES6 another two ways were added to declare variables which are ‘let’ and ‘const’.

Even though ‘var’ , ‘let’ and ‘const’ does the same job that’s declaring variables, there are some minor differences and there are some use cases which we can differently use these three.

Now let’s have a look about these three one by one :)

Var

Before ES6 there was only ‘var’, so the reason why ‘let’ and ‘const’ was introduced is because that the issues ‘var’ had.

The scope of ‘var’ is global or function. Scope is meant by wherever the variable is possible to be used.

When a var is declared outside a function or a block, then its scope is global.

In the above code, the ‘vehicle’ is globally scoped. So it can be accessed from anywhere through out the code. But the variable ‘country’ is function scoped, so it can only be accessed inside that particular function.

Also variables declared as var can be re declared updated.

In the above code the country has been updated and a new value has been assigned. In line 8, the currency has been declared with a new value. These two are possible with ‘var’.

Problem using ‘var’

In the above code, the variable country has been first assigned with the value ‘Sri Lanka’. In the if condition as the count is greater than 3, it gets true and the variable will be re declared with the new value of ‘England’.

If we do the re declaration willingly by knowing what is really happening, then it is not a problem. But if we re declare without knowing that it has already been declared before, we might run into confusion of getting different values for the variables that we didn’t assign values to.

This might cause bugs which are not easily identified in big systems.

‘Let’ and ‘const’ was introduced to overcome this problem so that developers can chose the type wisely and use it accordingly to their needs.

Let

After ES6 ‘let’ has taken the position of ‘var’ and now ‘let’ is widely used for variable declarations.

Let is block scoped. Block is area of code which is denoted inside { curly brackets }. Let can be updated but cannot be re declared.

In the above code count has been declared with a value 5. So the declaration happening in line 10 will cause error.

But the re declaration which is happening in line 8 will cause no error as it is in a different scope. That means the line 4 time cannot be re declared inside that block but can be re declared somewhere else outside the scope.

Const

Variables which are declared with const will be constant as the name suggests. It cannot be updated neither re declared.

Scope of const is same as let which is block scoped.

As in the code above, country and currency has been already declared in the top. So it cannot be updated or declared as in line 4 and 5.

But it is not the case with javascript object. The values inside a const object can be updated as below,

Even though we can update the values inside an object, the object itself cannot be re declared or updated as below,

Hope you had a good knowledge on the difference between var , let and const :). HMU if you have any clarifications.

Software Engineer Intern at 99x