DEV Community

Cover image for What is Temporal Dead Zone in JavaScript?
Dhairya Shah
Dhairya Shah

Posted on • Originally published at codewithsnowbit.hashnode.dev

What is Temporal Dead Zone in JavaScript?

Temporal Dead Zone is a term that describes the position where variables are unreachable;

Here,

  • Temporal means, temporary
  • Dead means, not in a working state
  • Zone means, the area where data is stored

The variables let and const exists in Temporal Dead Zone

TL;DR Temporal Dead Zone(TDZ) ends as soon as the respective variable is called.

/* 
    Temporal Dead Zone for the name variable
*/
const name = "SnowBit" // Haha, TDZ just ended 
console.log(name)
Enter fullscreen mode Exit fullscreen mode

Why was Temporal Dead Zone(TDZ) created?

//Temporal Dead Zone for the name variable
//Temporal Dead Zone for the name variable
//Temporal Dead Zone for the name variable
console.log(name) // Huh, I am in Temporal Dead Zone, let's get out of it
//Temporal Dead Zone for the name variable
//Temporal Dead Zone for the name variable
const name = "SnowBit" // Haha, TDZ just ended 
Enter fullscreen mode Exit fullscreen mode

As you can see, the name variable is declared in TDZ and will show error
tdz error.png


Thank you for reading, have a nice day!

Have a nice day.png

Top comments (1)

Collapse
 
ingosteinke profile image
Ingo Steinke

So far, so good ...

Why was Temporal Dead Zone(TDZ) created?

To elaborate on an answer, you could add an example of variable hoisting when using var instead of const and explain why it is a benefit to see an error message instead of working (unintendedly) with an undefined value.