← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

Temporal Dead Zone (TDZ)

JavaScript ES2015 Intermediate
debt(d3/e3/b3/t7)
d3 Detectability Operational debt — how invisible misuse is to your safety net

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.

e3 Effort Remediation debt — work required to fix once spotted

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.

b3 Burden Structural debt — long-term weight of choosing wrong

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.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

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.

About DEBT scoring →

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 99
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
3 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 1 ping S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 2 pings F 2 pings S 1 ping S 0 pings M
No pings yet today
Bing 1
Amazonbot 9 Ahrefs 7 SEMrush 6 Unknown AI 5 Google 5 PetalBot 5 Bing 4 Scrapy 4 Majestic 2 Perplexity 2 Claude 2 Sogou 2 Brave Search 2 Applebot 2 ChatGPT 1 Meta AI 1 Twitter/X 1
crawler 52 crawler_json 4 your_contextpost 1 pre-tracking 3
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
Hoisting javascript Hoisting is JavaScript's behavior of moving variable and function declarations to the top of their scope before code runs, so you can sometimes use them before you write them.

Hoisting explains confusing 'undefined' values and ReferenceErrors that beginners hit constantly. Understanding it teaches you why order and scope matter in JavaScript.

💡 Always declare variables and functions before you use them — don't rely on hoisting to save you.

Ask Codex about Hoisting →
JavaScript javascript The programming language of the browser — it reads and modifies the page (the DOM), reacts to user events, and fetches data without reloading.

JavaScript is the only language browsers execute, so every interactive behaviour on the web goes through it. Its two defining traits — single-threaded event loop and loose typing (== coercion) — explain the majority of both its bugs and its design patterns.

💡 Default to const, use === always, and reach for let only when a value genuinely reassigns.

Ask Codex about JavaScript →
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