EasyCore JS📖 Theory Question

What is the Temporal Dead Zone (TDZ)?

💡

Hint

let/const are hoisted but not initialized — accessing them before declaration throws

Full Answer

The Temporal Dead Zone is the period between when a let/const variable is hoisted (block start) and when it is initialized (the declaration line). Accessing it in this window throws a ReferenceError.

{
  // ← TDZ for 'a' starts here (block start)
  console.log(a); // ❌ ReferenceError: Cannot access 'a' before initialization
  let a = 5;      // ← TDZ ends, a is initialized to 5
  console.log(a); // 5
}

// typeof does NOT protect you in TDZ
console.log(typeof undeclared); // "undefined" — safe (never declared)
console.log(typeof a);          // ReferenceError if 'a' is in TDZ!

Why TDZ exists: Intentional design to catch bugs where you accidentally use a variable before its meaningful initialization. var silently returns undefined, masking errors.

💡 TDZ applies to let, const, and class declarations. Function declarations are fully hoisted with no TDZ.

More Core JS Questions

EasyWhat is the difference between var, let, and const?EasyExplain closures with a practical example.EasyWhat is hoisting in JavaScript?EasyExplain the event loop, call stack, and microtask queue.

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint