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.
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
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
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? |
|---|---|---|---|---|
|
| Function/global | Yes | Yes |
| ReferenceError (TDZ) | Block | No | Yes |
| ReferenceError (TDZ) | Block | No | No |
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/constreduce accidental early-use bugs.- Block scope makes loops/conditionals safer.
constcommunicates intent (binding should not change).
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)
if (true) {
var leaked = 1;
let scoped = 2;
}
console.log(leaked); // 1
// console.log(scoped); // ReferenceError
Follow-up rule of thumb
varis function-scoped and can leak outside blocks.let/constare block-scoped and stay in TDZ before initialization.constblocks 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."
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.
- Hoisting is common to all three.
- Initialization behavior is the key difference.
- Modern default:
constfirst,letwhen reassignment is needed, avoidvarin new code.
Use this as one explanation rep, then continue with the JavaScript interview questions cluster or a guided prep path.