Part 9 : let, const and Temporal Dead Zone in JS 🧐

Swati Redhu
7 min readJun 20, 2023

--

Prerequisite:

  1. Hoisting in javaScript
  2. β€œthis” keyword in JS

In this article, we’ll cover : :

  1. What is Temporal Dead Zone?
  2. Are let and const declaration hoisted?
  3. 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 :

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?

  1. const : whenever you put some value which is not going to change later, use const.
  2. let: As let has Temporal dead zone and you won’t run into unexpected errors.
  3. 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:

πŸš€πŸ‘‰ Clear your JS basics and find an amazing job

--

--

Swati Redhu

A developer attempting to learn the fundamentals and putting it here so that others could benefit from it.