var, let, and const are all hoisted, but they differ in initialization and access-before-declaration behavior. var initializes to undefined; let/const stay in TDZ until declaration executes.
Explain the difference in hoisting between `var`, `let`, and `const`
Quick Answer
Answer
Comparison answer
All three are hoisted. The real difference is what happens before declaration is reached.
Keyword | Before declaration access | Scope | Redeclare same scope? | Reassign? |
|---|---|---|---|---|
|
| Function/global | Yes | Yes |
| ReferenceError (TDZ) | Block | No | Yes |
| ReferenceError (TDZ) | Block | No | No |
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/constreduce accidental early-use bugs.- Block scope makes loops/conditionals safer.
constcommunicates 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)
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:
constfirst,letwhen reassignment is needed, avoidvarin new code.
Similar questions
Guides
Preparing for interviews?
Use the relevant interview-question hub first, then move into a concrete study plan before targeted company sets.