1. Home
  2. Interview Questions
  3. JavaScript
  4. 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

0 0
InterviewQuestions Team ✔ Expert answer ✔ Best 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:

DeclarationHoisted?Initial stateAccess before declaration
varYes (function scope)undefinedReturns undefined
function declarationYesThe full functionWorks
let / constYes (block scope)UninitialisedReferenceError
class declarationYes (block scope)UninitialisedReferenceError

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 typeof never 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 let declared inside one switch case 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 const differ 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

All hard questions →

Related Questions

‹
Q4 of 10 · JavaScript
Next ›

Interview Questions

Free interview questions and answers, MCQ practice, online tests, mock interviews and group discussions — everything you need to crack your next interview.
📍 Dasardot LLC
1309 Coffeen Avenue STE 1200
Sheridan, Wyoming 82801
USA