What is the difference between let, const and var?
Take a moment.
Say your answer out loud or write it down first — retrieval is what makes it stick. Reveal when you're ready.
Short answer
var is function-scoped and hoisted as undefined; let and const are block-scoped with a temporal dead zone. const additionally forbids reassignment.
Detailed explanation
Before ES6, var was the only option. It is scoped to the nearest function, gets hoisted to the top of its scope and initialized with undefined, which is why referencing it early doesn't throw. let and const are block-scoped: they exist only inside the nearest {} block. They are also hoisted, but not initialized — accessing them before their declaration line throws a ReferenceError, a period known as the Temporal Dead Zone (TDZ). The difference between let and const is reassignment: const bindings cannot be reassigned, though objects and arrays held by a const can still be mutated internally. Modern style guides recommend const by default, let when reassignment is needed, and avoiding var entirely.
Example
function demo() {
console.log(a); // undefined (hoisted)
// console.log(b); // ReferenceError (TDZ)
var a = 1;
let b = 2;
const c = [1, 2];
if (true) {
var a = 10; // same variable — function scope
let b = 20; // new variable — block scope
}
c.push(3); // allowed: mutation ≠ reassignment
// c = [9]; // TypeError: assignment to constant
}
demo();
console.log(a); // 10 — var leaked out of the blockInterview tip
Name all three differences explicitly: scoping, hoisting behavior, and reassignment rules. Mentioning the Temporal Dead Zone signals depth beyond memorized definitions.
Common mistakes
- Saying 'const makes objects immutable' — only the binding is fixed, the object contents can still change.
- Claiming let/const are not hoisted at all; they are hoisted but uninitialized (TDZ).