Temporal Dead Zone (TDZ)
debt(d3/e3/b3/t7)
Closest to 'default linter catches the common case' (d3). ESLint and TypeScript both detect TDZ violations automatically via rules like `no-use-before-define` and type checking, catching the most straightforward cases. However, some edge cases (like typeof on a TDZ variable, or complex control flow) may not be caught without specialist configuration.
Closest to 'simple parameterised fix' (e3). The quick_fix is straightforward: declare let/const at scope top and enable ESLint rules. Fixing TDZ violations typically requires moving declarations or reordering statements within a single component, not cross-file refactoring.
Closest to 'localised tax' (b3). TDZ is a language-level feature of let/const scoping that affects how developers write code within a single scope, but the burden is confined to individual blocks and functions. It doesn't create persistent architectural debt or poison the wider codebase—it's a local discipline issue.
Closest to 'serious trap' (t7). The misconception directly contradicts intuition: developers believe let/const are 'not hoisted' when they are actually hoisted but uninitialised. This contradicts var's hoisting behavior and the common assumption that typeof is always safe. The canonical gotcha (ReferenceError instead of undefined, class expressions in TDZ, typeof throwing) catches many developers during refactoring.
TL;DR
Explanation
TDZ is the period between entering a scope and reaching a variable's declaration. Unlike var (hoisted and initialised to undefined), let/const are hoisted but not initialised — accessing them in the TDZ throws ReferenceError: Cannot access 'x' before initialization. This is intentional — it prevents the var hoisting confusion. TDZ affects: let/const in blocks, class declarations (classes are in TDZ unlike function declarations), default parameter values that reference earlier parameters. typeof is the only operation that doesn't throw in the TDZ.
Common Misconception
Why It Matters
Common Mistakes
- Using a let variable before its declaration in the same block.
- Expecting class expressions to behave like function declarations (they're in TDZ).
- Not knowing typeof variable still throws in TDZ for let/const.
Code Examples
console.log(x); // ReferenceError — TDZ
let x = 5;
// Class TDZ:
const obj = new MyClass(); // ReferenceError
class MyClass {}
// Always declare before use:
let x = 5;
console.log(x); // 5
// Classes too:
class MyClass {}
const obj = new MyClass(); // Fine
// Safe check without TypeError:
if (typeof possiblyUndeclared !== 'undefined') {}