Learn JavaScript
❯ variables.tdz

The Temporal Dead Zone in JavaScript

$concept

The temporal dead zone is the span between entering a scope and executing a let/const declaration — touching the variable there throws ReferenceError (Cannot access before initialization) instead of yielding undefined. It's a deliberate safety feature: var's silent undefined masked ordering bugs, while the TDZ converts them into loud, located failures.

The zone covers the whole block from its top, which surprises people inside loops and switch cases (each case shares one block scope — declare in braces). typeof isn't exempt either: typeof x in the TDZ throws just like any other access.

$how it works
1

Declared but untouchable

{ console.log(x); let x = 1; } throws — the binding exists from block entry but refuses access until its line.

2

Safer than undefined

var's pre-line undefined silently computed wrong answers; TDZ errors point at the exact ordering mistake.

3

typeof throws too

The safe-check operator offers no shelter — typeof tdzVar before its line throws identically.

4

Order declarations first

Top-of-block lets plus no use-before-declare (lint rule) keep code permanently outside the zone.

$example
try {
console.log(value);
} catch (e) {
console.log("caught:", e instanceof ReferenceError);
}
let value = 10;
Output
caught: true
1Reading value before its let line hits the dead zone and throws ReferenceError.
2The catch proves the error class — a ReferenceError, not undefined, not TypeError.
3Had this been var, the same code would have printed undefined silently.
$use cases
Understanding Cannot access before initialization errors
Bracing switch cases so each gets its own TDZ-free scope
Enforcing declare-before-use with lint rules
Explaining why typeof offers no protection here

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.