Part 9 : let, const and Temporal Dead Zone in JS π§
Prerequisite:
In this article, weβll cover : :
- What is Temporal Dead Zone?
- Are let and const declaration hoisted?
- Difference between Syntax Error / Reference Error and Type Error?
β β β β β β β β β β β β β β β β β β β β β β β β β β β
Are let and const declaration hoisted?
Answer is : YES ππΌ
But they are hoisted very differently then var declarations.
In interviews, you can say that these are in Temporal Dead Zone.
Letβs understand taking an example:
let a = 10;
var b = 100;
In previous hoisting article, we learn that we can access variable βbβ even before we have initialised it. Like below
console.log(b);
let a = 10;
var b = 100;
Reason: Because in JS, memory has been allocated even before any line of code is executed. So, we can access variable βbβ even before initialising or declaring.
Output :
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β -
Question: What If I try to access variable βaβ in same way ? As it is hoisted , Will it give undefined too?
Answer is NO ππΌ
console.log(a);
let a = 10;
var b = 100;
Output:
SO, it is saying βReference Error: Cannot access βaβ before initialisationβ . i.e you can access βaβ only after you assigned or initialised some value to βaβ.
Letβs try below code:
let a = 10;
console.log(a);
var b = 100;
Output:
Now, you can see we were able to access βaβ. console prints value 10.
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β -
Question: How to know whether any variable is hoisted or not??
Letβs see that in browser, add a debugger:
Now, If you see both variables βaβ and βbβ are in scope with value undefined but βaβ is under βscriptβ where βbβ is in Global memory space.
To summarise: βletβ and βconstβ type variables will be allocated memory but that is not in Global Space. They are hoisted but not stored in global space. And this memory is not accessible until you put some value into that.
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
Question: What is Temporal Dead Zone??
It is the time since when let variable was hoisted and till it is initialised some value.
So, whenever you try to access any let or const variable in Temporal Dead Zone, it gives you Reference Error.
console.log(a);
let a = 10;
console.log(a);
var b = 100;
So, In above example, until Line 2 : let a = 10; . βaβ is in Temporal Dead Zone and gives you Reference Error at Line 1.
After Line 2, at Line 3: you can access variable a.
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
In previous article of βthisβ keyword in JS, we tried to access variables using βthisβ and βwindowβ keywords. Letβs just try that too.
As you can see above, we are not able to access βaβ using βwindowβ and βthisβ keyword. It keeps giving βundefinedβ.
Reason: βthisβ and βwindowβ represents global space and memory allocated to βaβ was different then global object. So, this shows, βletβ is strict then βvarβ.
β β β β β β β β β β β β β β β β β β β β β β β β β β -
Question: Can we re-declare βletβ variable?
Answer is NO ππΌ
let a = 10;
let a = 100;
Output:
SO, it is saying βSyntax Error: Identifier βaβ has already been declaredβ .
Also, JS will not run any line of code if it sees redeclaration. It will not reach any line of code.
what If I try below ?
let a = 10;
var a = 100;
Output:
Same again: βSyntax Error: Identifier βaβ has already been declaredβ . βletβ variable canβt be declared again with same name again in same scope.
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β -
Question: Can we re-declare βvarβ variable?
Answer is YES ππΌ
var a = 10;
var a = 100;
No error when you run code.
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β -
Question: Can we re-declare βconstβ variable?
Answer is NO ππΌ
var a = 10;
const b= 100;
const b= 1000;
Output:
βconstβ variable canβt be declared again with same name in same scope.
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β -
What If I run below code?
let a;
const b;
var c;
a = 10;
b = 100;
c = 1000;
SO, it is saying βSyntax Error: Missing Initializer in const declaration.β This means βconstβ variable needs to be intialised and declared in same line.
βconst needs to be initialised and declared in same lineβ
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β -
What If I try to assign some value again to βconstβ variable?
let a = 10;
const b = 100;
a = 20;
b = 200;
SO, it is saying βType Error: Assignment to constant variable.β You can not assign value again to βconstβ type variable.
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β -
Now, As we have three different ways to declare a variable using let, var and const.
Which one we should use?
- const : whenever you put some value which is not going to change later, use const.
- let: As let has Temporal dead zone and you wonβt run into unexpected errors.
- var: Try to put it aside and not use.
β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
How to avoid Temporal Dead Zone errors?
Try to put all declaration and initialisation on top of scope. This will minimise unexpected errors when logic get called.
If you still have any queries related to hoisting of var, let and const. Please write down in comments.
βοΈHappy Coding. βοΈ
Hopefully, you learned something today! Before you go:
- π Clap for the story and follow Swati Redhu π©π»βπ»
- π° View more content in the JavaScript learning roadmap 2023 β The Ultimate Guide β€οΈ
- π Follow on : LinkedIn