The differences between var and let and const in Javascript

In this post we take a look at the differences between var and let and const in Javascript. There are confusions sometimes of where to use which and what are the best practices.

To declare a variable we used to use var before ES6 introduced let. With var, you can declare a variable anywhere and redeclare it again meaning you can change its value by redeclaring it. Because var is function scope but let and const are block scope which means they scope inside curly braces.

Example of let and var

Lets take a look at this example I made:

var

For var variable, we can redeclare it by reassigning it another value and it always works fine.It has changed from 2 to 3.  It will cause problems though. In applications with lots if declarations and variables, var is buggy and no good to use.

let

With lee, we just cannot redeclare a variable. This is why let has been introduced. It is there so if accidentally a variable has been redeclared inside block scope, we make sure its values will not change. This is really good and reliable. And if you do redeclare a let variable. it gives you an error and your application will not run. It says this variable has been declared before. ( You can see in the example we cannot redeclare the name variable to “John”. Let is great to be used in loops like for loops as the value keep changing and it is more reliable.

But in the other hand, we can change the value of let variable to something else. This is great as we actually know what we are doing and we will not make mistakes.

const

After those comes const which is a data type that its value cannot be changed at all. It is not immutable as they say. Although a const value cannot be changed but its properties value could be changed. Meaning if you have a const object with some attributes, you can change its values by reassigning new value to them.

In the example you can see that we cannot change the “team” value as it gives the type error ” the const has been declared before”. But we can reassign the value of person.hobby to “Coding” as we are not changing the const value itself but its attribute’s values.

Where to use which

So if we need a constant value to keep it still and not change its existence, we should use const.

If we need a variable that its value could be changed inside block scope but could not be redeclare, we should consider let.

If we need a variable that should be redeclared inside function scope, we need to use var. But it is not recommended as it causes unwanted problems and accidental mistakes.

© 2020
Azadeh Faramarzi

This site is created and maintined by Azadeh Faramarzi , A passionate developer, sport and code lover who loves to share and write when she has some time.