Member-only story

What is temporal dead zone in JavaScript ?

Pravin M
2 min readNov 30, 2023

--

What is temporal dead zone in JavaScript
What is temporal dead zone in JavaScript

Source: What is temporal dead zone in JavaScript ?

For more questions and answers visit our website at Frontend Interview Questions

The Temporal Dead Zone (TDZ) is a concept in JavaScript that refers to a specific period during the execution of a program where a variable exists but cannot be accessed. This period occurs between the start of a scope (function or block) and the point where the variable is declared using the `let` or `const` keywords. The TDZ prevents developers from accessing variables before they are initialized, helping to catch potential issues related to variable hoisting and ensuring better code reliability.

Understanding the Temporal Dead Zone:

Let’s look at an example to understand the Temporal Dead Zone:


console.log(x); // Output: ReferenceError: Cannot access ‘x’ before initialization
let x = 10;

In this example, we are trying to access the variable `x` before it is declared and initialized. The code throws a `ReferenceError` during runtime because `x` is in the Temporal Dead Zone at the time of the `console.log` statement.

Temporal Dead Zone and `let` and `const` Declarations:

Variables declared using `let` and `const` are hoisted to the top of their scope. However…

--

--

Pravin M
Pravin M

Written by Pravin M

I am a frontend developer with 10+ years of experience Blog :- https://www.frontendinterviewquestions.com

No responses yet