What is the Temporal Dead Zone in JavaScript?
Understand the JavaScript Temporal Dead Zone: why let and const throw a ReferenceError before declaration, how hoisting differs from var, with examples.
Expected Interview Answer
The Temporal Dead Zone (TDZ) is the period between when a let or const variable enters scope and when it is actually declared, during which accessing the variable throws a ReferenceError.
Variables declared with let and const are hoisted to the top of their block like var, but unlike var they are not initialised to undefined. They stay in an uninitialised 'dead zone' from the start of the block until the line where they are declared executes. Any read or write during that window throws a ReferenceError, which is why the zone is 'temporal' — it is defined by time of execution, not position in the code. This behaviour helps catch use-before-declaration bugs that var would silently allow.
- Catches use-before-declaration bugs instead of returning undefined
- Makes const truly constant from the moment of declaration
- Encourages declaring variables before using them
- Provides clearer, more predictable scoping than var
- Surfaces typos and ordering mistakes as loud errors
AI Mentor Explanation
The TDZ is like a substitute fielder named in the team sheet but not yet cleared by the umpires to take the field. The player exists on paper the moment the innings starts, yet if the captain tries to use them before official clearance, the umpire refuses — access is denied until the exact moment they are formally brought on.
Step-by-Step Explanation
Step 1
Block scope starts
When execution enters a block, let and const names in it are hoisted and reserved, but left uninitialised.
Step 2
Dead zone begins
From the top of the block until the declaration line, the variable is in the TDZ and any access throws a ReferenceError.
Step 3
Declaration executes
When the line with let or const runs, the variable is initialised (to its value or undefined for a bare let).
Step 4
Dead zone ends
After initialisation the variable can be read and written normally for the rest of the block.
Step 5
Contrast with var
var is initialised to undefined at hoist time, so it has no TDZ and returns undefined instead of throwing.
What Interviewer Expects
- That let and const are hoisted but not initialised
- Why accessing them early throws a ReferenceError, not undefined
- The difference in hoisting behaviour between var and let/const
- Understanding 'temporal' refers to execution time, not source position
- A code example that triggers the TDZ error
Common Mistakes
- Claiming let and const are not hoisted at all
- Expecting undefined instead of a ReferenceError when accessing early
- Thinking the TDZ is a location in code rather than a period in time
- Confusing TDZ errors with plain 'variable is not defined' typos
- Believing typeof on a TDZ variable is safe (it also throws)
Best Answer (HR Friendly)
“The temporal dead zone is the gap between where a let or const variable's block starts and where the variable is actually written in the code. If you try to use the variable in that gap, JavaScript stops you with an error, which helps catch bugs where you use something before it is set up.”
Code Example
{
// TDZ for 'price' starts here
console.log(typeof price); // ReferenceError, not 'undefined'
console.log(price); // ReferenceError: Cannot access 'price' before initialization
let price = 100; // TDZ ends here; 'price' is now usable
console.log(price); // 100
}
// Contrast with var — no TDZ
{
console.log(qty); // undefined (hoisted and initialised)
var qty = 5;
console.log(qty); // 5
}Follow-up Questions
- Are let and const hoisted? Explain what actually happens.
- Why does typeof throw for a TDZ variable but not for a truly undeclared one?
- How does the TDZ differ from var's behaviour?
- Does function-scope or block-scope define the TDZ boundaries?
- How can the TDZ help you catch bugs during development?
MCQ Practice
1. What happens when you access a let variable before its declaration line runs?
let and const are hoisted but uninitialised, so accessing them in the TDZ throws a ReferenceError.
2. Why is it called the 'temporal' dead zone?
The zone is bounded by when code executes relative to the declaration, making it a matter of time, not location.
3. Which declaration does NOT create a temporal dead zone?
var is initialised to undefined when hoisted, so it has no TDZ; let, const, and class do.
Flash Cards
What is the Temporal Dead Zone? — The span from a block's start to a let/const declaration where accessing the variable throws a ReferenceError.
Are let and const hoisted? — Yes, but they stay uninitialised until their declaration line, unlike var which becomes undefined.
What does typeof do on a TDZ variable? — It throws a ReferenceError — typeof is not safe inside the TDZ.
Does var have a TDZ? — No. var is initialised to undefined at hoist time, so it returns undefined instead of throwing.