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

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (1)

Collapse
 
ingosteinke profile image
Ingo Steinke, web developer

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.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay