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

HighIntermediateJavascript
Quick Answer

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.

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?

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)
                  

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 the relevant interview-question hub first, then move into a concrete study plan before targeted company sets.