- Home
- Interview Questions
- JavaScript
- What is the temporal dead zone, and are let and const actual…
Try answering first
In a real interview nobody shows you the answer. Think it through (or type it), then reveal the answers and rate yourself.
1 Answer
Short answer: Yes, let, const and class declarations are hoisted: their bindings are created when the scope is entered. Unlike var, they are not initialised. The period between entering the scope and executing the declaration is the temporal dead zone (TDZ), and any access during it throws a ReferenceError.
Detailed explanation
When JavaScript enters a scope (a function, block or module), it first registers every declaration in that scope. What happens next depends on the declaration type:
| Declaration | Hoisted? | Initial state | Access before declaration |
|---|---|---|---|
| var | Yes (function scope) | undefined | Returns undefined |
| function declaration | Yes | The full function | Works |
| let / const | Yes (block scope) | Uninitialised | ReferenceError |
| class declaration | Yes (block scope) | Uninitialised | ReferenceError |
The binding leaves the TDZ only when execution reaches the declaration line. For let x; with no initialiser, it becomes undefined at that point.
The clearest proof that let is hoisted is shadowing. If an inner block declares let x, reading x earlier in that block does not fall back to the outer x. It throws, because the inner binding already exists but is uninitialised.
The zone is temporal, meaning it depends on time, not on position in the source. A function body may mention a variable declared lower in the file. That is fine, as long as the function is called after the declaration has run.
Why the design? It catches use-before-assignment bugs early. It also keeps const meaningful: without the TDZ, a const could be observed as undefined first and as its real value later, which would make it effectively change.
Example
// 1. Shadowing proves hoisting
let x = 'outer';
{
// console.log(x); // ReferenceError: Cannot access 'x' before initialization
let x = 'inner';
}
// 2. typeof is not safe inside the TDZ
console.log(typeof notDeclared); // 'undefined'
// console.log(typeof later); // ReferenceError
let later = 1;
// 3. Temporal, not spatial
function read() { return value; }
// read(); // ReferenceError if called here
const value = 42;
console.log(read()); // 42
// 4. Default parameters have their own TDZ
function f(a = b, b = 1) { return a; }
// f(); // ReferenceError: b is in TDZ
function g(a = 1, b = a) { return b; }
console.log(g()); // 1
// 5. Self-reference
// let y = y + 1; // ReferenceError
// 6. Classes too
// new Car(); // ReferenceError
class Car {}A practical place this bites is a switch statement. All case clauses share one block. A let declared in one case is therefore in the TDZ for code in other cases. Wrap each case body in braces to give it its own block.
Common mistakes
- Saying "let and const are not hoisted". They are hoisted but not initialised. The shadowing example is the counter-argument interviewers look for.
- Assuming
typeofnever throws. It returns 'undefined' for undeclared names but throws a ReferenceError for a variable in its TDZ. - Thinking the TDZ depends on line order alone. What matters is whether the declaration has executed at the moment the variable is accessed.
- Forgetting that classes have a TDZ. Unlike function declarations, a class cannot be used before its declaration.
Follow-up questions
- Why does a
letdeclared inside oneswitchcase cause errors in another case, and how do you fix it? - What is the difference between "x is not defined" and "Cannot access 'x' before initialization"?
- How do function expressions assigned to
constdiffer from function declarations in terms of hoisting?
Interview tip: Answer "hoisted but uninitialised", then prove it immediately with the shadowing example. That one snippet shows you understand the mechanism rather than a memorised rule.
Reviewed by the Interview Questions editorial team · Last updated
How well did you know this?
Rate yourself to track progress, build your streak and get “didn't know” questions in your revision list.
Your Answer
More hard JavaScript questions
- How do HttpOnly, Secure and SameSite cookie flags protect against XSS and CSRF? · 1 ans
- Why can't try/catch catch an error thrown inside a setTimeout callback or a promise? · 1 ans
- What are tagged template literals in JavaScript and when would you actually use one? · 1 ans
- Why is using await inside a loop often slow, and how do you run async tasks in parallel? · 1 ans
Related Questions
- Why can you call a function declaration before it is defined but not a function expression? · JavaScript · 1 ans
- Why does a for loop with var and setTimeout print the same value every time? · JavaScript · 1 ans
- Why do lambdas created inside a loop all return the last loop value in Python? · Python · 1 ans
- What are tagged template literals in JavaScript and when would you actually use one? · JavaScript · 1 ans
- How do try, catch and finally work, and what happens if finally contains a return? · JavaScript · 1 ans
- Why can't try/catch catch an error thrown inside a setTimeout callback or a promise? · JavaScript · 1 ans
- What is the difference between localStorage, sessionStorage and cookies in the browser? · JavaScript · 1 ans
- How do HttpOnly, Secure and SameSite cookie flags protect against XSS and CSRF? · JavaScript · 1 ans
🙋 Was this asked in your interview?
Which company? (optional — adds this question to the company's page)