What is the temporal dead zone?
Quick Answer
The temporal dead zone (TDZ) is the region from the start of a block until a let/const declaration is reached. The variable is hoisted (the scope knows about it) but uninitialised, so referencing it in the TDZ throws ReferenceError: Cannot access 'x' before initialization — unlike var, which would just be undefined.
The temporal dead zone is the gap between entering a block and reaching a let or const declaration. The variable is technically hoisted — the block knows it exists — but it's not initialised, so if you touch it in that gap you get a ReferenceError, not undefined like you would with var. Even typeof throws in there. It's actually a good thing — it stops you accidentally using a variable before it's been given a value.
- Even
typeof xthrows in the TDZ (forvarit returns"undefined"). - The TDZ is why
const/letfeel safer — you can't accidentally use a variable before it's set up. - It's per-block:
{ console.log(y); let y = 1; }throws; the outer scope'syis not consulted.