Facebook Pixel

TDZ Interview Questions for let and const in JavaScript

The TDZ is a favorite interview topic. Here are the most asked questions with answers and code.

TDZ Interview Questions for let and const in JavaScript

The Temporal Dead Zone is a favorite interview topic. Here are the common questions, with answers and code.

Q1: What is the output?

console.log(x); let x = 5; `` **Answer**: `ReferenceError: Cannot access 'x' before initialization`. `x` is in the TDZ. ### Q2: What is the output? ```js console.log(y); var y = 5; `` **Answer**: `undefined`. `var` is hoisted with `undefined`. ### Q3: What is the output? ```js let x = 1; { console.log(x); let x = 2; } `` **Answer**: `ReferenceError`. The inner `let x` is hoisted (shadowing the outer), so `x` in the block is in the TDZ until the inner declaration line. It does not fall back to the outer `x`. ### Q4: What is the output? ```js console.log(typeof a); let a; `` **Answer**: `ReferenceError`. Even `typeof` cannot escape the TDZ. ### Q5: What is the output? ```js function foo(a = b, b = 2) { console.log(a, b); } foo(); `` **Answer**: `ReferenceError: Cannot access 'b' before initialization`. `b` is in the TDZ when `a`'s default is evaluated (defaults run left to right). ### Q6: What is the output? ```js for (let i = i; i < 3; i++) {} `` **Answer**: `ReferenceError`. The right-hand `i` is in the TDZ until the declaration completes. ### Q7: What is the output? ```js const x; `` **Answer**: `SyntaxError: Missing initializer in const declaration`. This is a parse-time error, not a TDZ error. ### Q8: What is the output? ```js const x = 5; x = 10; `` **Answer**: `TypeError: Assignment to constant variable`. The variable exists (no `ReferenceError`), but reassignment is not allowed. ### Tips for Interviews - TDZ applies to `let`, `const`, `class`, and default parameters. - `typeof` does NOT escape the TDZ (unlike undeclared variables). - `var` is never in the TDZ; it is initialized to `undefined`. - Shadowing + TDZ: an inner `let` shadows the outer from the start of the block. - `const` without an initializer is `SyntaxError`, not TDZ. ### The Takeaway TDZ interview questions test: early access to `let`/`const`, `typeof` in the TDZ, shadowing + TDZ, default parameter TDZ, loop TDZ, `const` without initializer (SyntaxError), and `const` reassignment (TypeError). Walk through the memory phase and the TDZ for each `let`/`const`.

ReferenceError: Cannot access 'x' before initialization. The message is specific to the TDZ, different from 'x is not defined' which means the variable does not exist at all.

No. typeof on a let or const in the TDZ throws ReferenceError. This is different from typeof on an undeclared variable, which returns 'undefined' without throwing.

Because the inner let is hoisted (memory allocated) at the start of the block, so it shadows the outer immediately. But it is in the TDZ until its declaration line, so accessing it early throws instead of falling back to the outer.

They are evaluated left to right. A parameter that references a later parameter throws ReferenceError because that later parameter is still in the TDZ. For example, function foo(a = b, b = 2) throws.

const x; (no initializer) is a SyntaxError caught at parse time. const x = 5; x = 10; (reassignment) is a TypeError thrown at runtime. Different error types because they fail at different stages.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.