Hoisting and Temporal Dead Zone
What common mistakes do candidates make around Hoisting and Temporal Dead Zone, and how do you avoid them?
Answers use simple, clear English.
Audio N/AQuick interview answer
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.
Detailed answer
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. 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: Mid-level.
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?