JavaScript · The language round
What is the difference between var, let and const?
Short answer
var is function-scoped and initialised to undefined when hoisted. let and const are block-scoped and stay in the temporal dead zone until their declaration runs. const prevents reassignment, not mutation.
The interview answer has three parts: scope, hoisting and reassignment. Most candidates stop at "const means constant", which is where the follow-up lands.
Scope
function scopeDemo() {
if (true) {
var functionScoped = "var";
let blockScoped = "let";
}
console.log(functionScoped); // "var"
console.log(blockScoped); // ReferenceError
}var belongs to the nearest function, so it escapes the if block. let and const belong to the nearest block, which makes them safer for loops and conditionals.
Hoisting and the temporal dead zone
console.log(a); // undefined
var a = 1;
console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 2;var is hoisted and initialised to undefined. let and const are also hoisted, but they are not initialised until execution reaches the declaration. The period between entering the scope and initialisation is the temporal dead zone.
Reassignment vs mutation
const user = { name: "Asha" };
user.name = "Ravi"; // allowed: mutating the object
user = {}; // TypeError: assignment to a constant bindingconst locks the binding, not the value. Use const by default, switch to let only when the variable is reassigned, and avoid var in new code.
Common mistakes
- Saying
constmakes an object immutable. UseObject.freeze()for shallow immutability when you need it. - Saying
letandconstare not hoisted. They are hoisted but remain uninitialised. - Using
varinside a loop with callbacks, then wondering why every callback sees the final index.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Is a `const` object immutable?
- 2What is the temporal dead zone?
- 3Do `let` and `const` hoist?
- 4Why does `var` inside a loop behave differently from `let`?
Say it in the interview
`var` is function-scoped, hoisted and initialised to undefined. `let` and `const` are block-scoped and sit in the temporal dead zone until their declaration executes. `const` prevents reassigning the binding but does not freeze the object, so properties can still change. I default to `const`, use `let` only for reassignment, and avoid `var` in new code.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live JavaScript mock interview and get scored feedback on depth, clarity and confidence.