← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Temporal Dead Zone (TDZ)

javascript ES2015 Intermediate

TL;DR

let and const variables exist in a Temporal Dead Zone from the start of their block until their declaration — accessing them before throws ReferenceError.

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

let and const are not hoisted — they ARE hoisted (the engine knows they exist in the scope) but remain uninitialised until the declaration line.

Why It Matters

TDZ errors are a common gotcha when refactoring var to let/const — code that relied on var hoisting breaks with ReferenceError instead of getting undefined.

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

✗ Vulnerable
console.log(x); // ReferenceError — TDZ
let x = 5;

// Class TDZ:
const obj = new MyClass(); // ReferenceError
class MyClass {}
✓ Fixed
// 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') {}

Added 22 Mar 2026
Views 20
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S
No pings yet today
Amazonbot 7 Unknown AI 3 Perplexity 2 Google 2 ChatGPT 1 Majestic 1 Ahrefs 1
crawler 13 crawler_json 1 pre-tracking 3
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Always declare let/const at the top of their scope. If converting from var, check all usage is after the declaration. Enable ESLint no-use-before-define rule.
📦 Applies To
javascript ES2015 web cli
🔗 Prerequisites
🔍 Detection Hints
let |const
Auto-detectable: ✓ Yes eslint typescript
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Medium ✗ Manual fix Fix: Low Context: Function

✓ schema.org compliant