Let, Const, Var — Difference

Shivansh Mehrotra
2 min readJun 30, 2021

We can declare variables in three ways in javascript. These are const, let, and var. Const is the most strict way, let is the lesser strict way and var is the default way.

Difference between let, const and var

Anybody who knows the basics of javascript knows that variables are hoisted in javascript. By hoisting, we mean all types of variables are hoisted including let & const also.

By hoisting, we mean var type variables are allocated memory space in the execution context with the value undefined.

If we try to print the value of b before declaring it will print undefined because it is already allocated a memory space hence showing undefined and not giving an error. But in the case of let and const, they are also hoisted but not in the global execution context. But in separate memory space.

Let is hoisted but is in a state called the temporal dead zone

Let is in a state called the temporal dead zone. Hence it is showing the error “a is not defined.

After initializing variable ‘a’ is out of the temporal dead zone

After giving value to the variable, we can see both console.log statements are printing the values.Let variables are stored in separate memory space and they are out of the temporal dead zone once they are initialized.

Const is the most strict version of declaring a variable. As the name suggests, after declaring a variable you cannot change its values or you can’t redeclare it.

With const, you are supposed to initialize the variable at the time of declaration. We can say, const is similar to let in terms of hoisting in addition that we can’t change its value once declared.

Conclusion

var : no temporal dead zone, can redeclare and re-initialize and stored in Global execution context.

let : temporal dead zone, cannot redeclare but we can re-initialize and stored in separate memory.

const: temporal dead zone, more strict , temporal dead zone, can’t redeclare, can’t re-initialize and stord in a separate memory.

--

--