Interview answer drill

Use this JavaScript interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.

Explain the difference in hoisting between `var`, `let`, and `const`Frontend interview answer

HighIntermediateJavascript
Interview focus

This JavaScript interview question tests whether you can explain var vs let vs const hoisting: TDZ, scope, and real bug patterns, connect it to production trade-offs, and handle common follow-up questions.

  • var vs let vs const hoisting: TDZ, scope, and real bug patterns explanation without falling back to memorized docs wording
  • Hoisting and Variables reasoning, edge cases, and production failure modes
  • How you would answer the most likely JavaScript interview follow-up
Practice more JavaScript interview questions
Interview quick answer

var, let, and const are all hoisted, but interviewers usually care about three axes at once: access-before-declaration behavior, scope boundaries, and redeclaration/reassignment rules. Strong answers connect those rules to loop bugs and accidental leakage.

Full interview answer

Comparison answer

All three are hoisted. The real difference is what happens before declaration is reached. High-signal answers also mention scope boundaries and redeclaration rules, because that is where production bugs show up.

Keyword

Before declaration access

Scope

Redeclare same scope?

Reassign?

var

undefined

Function/global

Yes

Yes

let

ReferenceError (TDZ)

Block

No

Yes

const

ReferenceError (TDZ)

Block

No

No

Cheat sheet interviewers expect you to know cold.
JAVASCRIPT
console.log(x); // undefined
var x = 10;

console.log(y); // ReferenceError
let y = 10;

console.log(z); // ReferenceError
const z = 10;
                  

Why this matters in production code

  • let/const reduce accidental early-use bugs.
  • Block scope makes loops/conditionals safer.
  • const communicates intent (binding should not change).
JAVASCRIPT
for (let i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0);
}
// 0 1 2 (expected)

for (var j = 0; j < 3; j++) {
  setTimeout(() => console.log(j), 0);
}
// 3 3 3 (common var trap)
                  
JAVASCRIPT
if (true) {
  var leaked = 1;
  let scoped = 2;
}

console.log(leaked); // 1
// console.log(scoped); // ReferenceError
                  

Follow-up rule of thumb

  • var is function-scoped and can leak outside blocks.
  • let/const are block-scoped and stay in TDZ before initialization.
  • const blocks rebinding, not internal object mutation.

Interview-ready one-liner
"They are all hoisted, but only var is initialized early; let/const stay in TDZ and throw on access before declaration."

Still so complicated?

var gives you a key immediately (value may be empty). let/const reserve the room but keep it locked until you arrive at that line.

Summary
  • Hoisting is common to all three.
  • Initialization behavior is the key difference.
  • Modern default: const first, let when reassignment is needed, avoid var in new code.
Similar questions
Guides
Preparing for interviews?

Use this as one explanation rep, then continue with the JavaScript interview questions cluster or a guided prep path.