Hoisting and Temporal Dead Zone
As a Fresher engineer, explain Hoisting and Temporal Dead Zone. What problem does it solve and how would you describe it in an interview?
Answers use simple, clear English.
Quick interview answer
At Fresher depth: start with the problem, then mechanism, then a short example. var hoists and initializes undefined; let/const hoist but stay in TDZ until declaration — early access throws ReferenceError.
Detailed answer
At Fresher depth: start with the problem, then mechanism, then a short example. var hoists and initializes undefined; let/const hoist but stay in TDZ until declaration — early access throws ReferenceError. const prevents rebinding, not deep mutation. Core: var hoists and initializes undefined; let/const hoist but stay in TDZ until declaration — early access throws ReferenceError. const prevents rebinding, not deep mutation. Real-time example: Loop with var captures same binding; let creates per-iteration binding in for loops. Pros: let/const reduce subtle bugs; block scope matches developer intuition. Cons: TDZ surprises in temporal code; const does not freeze object contents. Common mistakes: Assuming const makes objects immutable; using var in modern modules. Best practices: Default to const; use let when reassignment needed; never var in new code. Audience level: Fresher.
Full explanation
var hoists and initializes undefined; let/const hoist but stay in TDZ until declaration — early access throws ReferenceError. const prevents rebinding, not deep mutation.
Real example & use case
Loop with var captures same binding; let creates per-iteration binding in for loops.
Pros & cons
Pros: let/const reduce subtle bugs; block scope matches developer intuition. Cons: TDZ surprises in temporal code; const does not freeze object contents.
Common mistakes
Assuming const makes objects immutable; using var in modern modules.
Best practices
Default to const; use let when reassignment needed; never var in new code.
Follow-up questions
- How would you test Hoisting and Temporal Dead Zone?
- What metrics prove Hoisting and Temporal Dead Zone is healthy in prod?
- How does Hoisting and Temporal Dead Zone change at 10× traffic?