Temporal Dead Zone is the time between:
(1) when a variable is declared (in memory)
and
(2) when it is actually initialized
During this time, the variable exists, but cannot be accessed.
Trying to use it results in a ReferenceError.
🧠 Why Does TDZ Happen?
Variables declared with let and const are hoisted, but they are not initialized automatically like var.
var→ hoisted & initialized toundefined(no TDZ)let/const→ hoisted but not initialized → TDZ exists
🧪 Example of TDZ
console.log(a); // ❌ ReferenceError (TDZ)
let a = 10;
Even though a is hoisted, you cannot access it before the line let a = 10.
🔥 TDZ With const
Same behavior, but stricter because const must be initialized immediately:
console.log(x); // ❌ ReferenceError
const x = 5;
🧠 Real Explanation
When JavaScript executes code, it creates a scope and allocates memory for variables.
But:
letandconstare placed in a "temporal dead zone" until execution reaches their line.- Accessing them before initialization → ReferenceError
- This ensures safer and predictable code.
🛑 Example Showing TDZ Clearly
function demo() {
console.log(msg); // ❌ TDZ error
let msg = "Hello";
}
demo();
Why?
Because JavaScript knows msg exists in this scope,
but it is not initialized until the let line is executed.
🎯 Example: var vs let (TDZ)
console.log(a); // undefined (no TDZ)
var a = 10;
console.log(b); // ReferenceError (TDZ)
let b = 20;
👇 Relation Between TDZ, let, and const
| Feature | var | let | const |
|---|---|---|---|
| Hoisted | Yes | Yes | Yes |
| Initialized on hoist | Yes (undefined) | No | No |
| Temporal Dead Zone | ❌ No | ✔ Yes | ✔ Yes |
🎯 Short Interview Answer
The Temporal Dead Zone is the period between variable creation and initialization where
letandconstvariables cannot be accessed.
These variables are hoisted but not initialized, so accessing them before their declaration causes a ReferenceError.vardoes not have a TDZ because it is automatically initialized toundefined.