JavaScript Interview Prep
JS Engine & Execution

Temporal Dead Zone

The ReferenceError That Saves Your Bugs

Scenario

The Temporal Dead Zone sounds like a Christopher Nolan movie. It's actually one of the best features let and const gave us.

It's the window of time between when a let/const variable enters scope and when its declaration line actually runs. During that window, the variable exists in memory - but touching it throws a ReferenceError.

Why would the language designers do this on purpose? Because var's "silent undefined" behavior hid bugs for YEARS. TDZ makes those bugs loud and immediate.


The No-Touch Window

The Temporal Dead Zone is the period between when a let/const variable enters scope and when it's actually declared in the code.

During this window, the variable exists in memory but is uninitialized. Accessing it throws a ReferenceError.

Code Example

{
    // TDZ for `name` starts here
    console.log(name); // ReferenceError: Cannot access 'name' before initialization

    // TDZ continues...

    let name = "Rakibul"; // TDZ ends here
    console.log(name); // "Rakibul" - works fine now
}

Why Does TDZ Exist?

TDZ catches bugs early. With var, you'd get undefined silently - and spend hours debugging why your variable is undefined instead of the value you expected.

// BAD: var hides bugs
console.log(user); // undefined - no error, silent bug
var user = "Admin";

// GOOD: let catches the bug immediately
console.log(user); // ReferenceError - you know instantly
let user = "Admin";

TDZ in Function Parameters

// TDZ can bite you in default parameters too!
function greet(a = b, b = 2) {
    console.log(a, b);
}
greet(); // ReferenceError: Cannot access 'b' before initialization

// Fix: reverse the order
function greet(b = 2, a = b) {
    console.log(a, b); // 2, 2
}

TDZ with typeof

// var: typeof works even before declaration
console.log(typeof undeclaredVar); // "undefined" - no error

// let: typeof STILL throws in TDZ
console.log(typeof myLet); // ReferenceError
let myLet = 10;

This is one of the rare cases where typeof can throw an error.


Common Mistakes

  • Assuming typeof variable is always safe - inside the TDZ, even typeof throws a ReferenceError.
  • Referencing a default parameter that appears later in the same parameter list - parameters are evaluated left-to-right, so the right-hand one is still in its TDZ.
  • Expecting let/const to silently be undefined before their declaration - they are hoisted but uninitialized; reading them is an error, by design.

Interview Questions

Q: What is the Temporal Dead Zone?

The period between when a let/const variable is hoisted into its scope and when its declaration line executes. Accessing the variable during this window throws ReferenceError: Cannot access 'X' before initialization.

Q: Does typeof work safely with variables in the TDZ?

No. Unlike undeclared variables where typeof returns "undefined" safely, variables in the TDZ will cause typeof to throw a ReferenceError. This is because the variable is known to exist (it's hoisted) but is explicitly uninitialized.

Q: How is TDZ different from var's hoisting behavior?

var is hoisted AND initialized to undefined, so reading it before its assignment gives undefined silently. let/const are hoisted but left uninitialized - reading them during the TDZ throws a ReferenceError, which surfaces bugs loudly instead of hiding them.

Q: Why was the TDZ added to the language?

To catch use-before-declaration bugs at runtime. var's silent undefined hid these bugs for years; TDZ makes them immediate, diagnosable errors.

On this page