What is Temporal Dead Zone (TDZ) in JavaScript
What is Temporal Dead Zone (TDZ)?
TDZ (Temporal Dead Zone) is the time period between the start of a variable's scope (e.g., if block, for, function, etc.) and its actual initialization, during which the variable cannot be accessed.
This zone occurs when using let or const.
Simple Example
console.log(x); // ❌ ReferenceError: Cannot access 'x' before initialization
let x = 10;
Although variable x is declared, you get an error because you accessed it before initialization — you're in the temporal dead zone.
TDZ applies to:
letconst- Function parameters with destructuring
- Classes declared via
class
Conclusion
| Variable Type | Hoisted | Initialized | TDZ Exists |
|---|---|---|---|
var | Yes | undefined | No |
let | Yes | No | Yes |
const | Yes | No | Yes |
Important:
TDZ is the reason why let and const are safer than var: you cannot use variables before they're declared, and this prevents errors.