What is the difference between var, let and const?
Quick answer
`var` is function-scoped and hoisted as `undefined`; `let` and `const` are block-scoped with a temporal dead zone. `const` can't be reassigned.
In detail
`var` is scoped to the nearest function and is hoisted and initialised to `undefined`, so it can be referenced (as undefined) before its line. `let` and `const` are scoped to the nearest block and live in the Temporal Dead Zone until declared, so using them early throws a ReferenceError. `const` must be initialised and cannot be reassigned — though for objects/arrays the contents can still be mutated.
1console.log(a); // undefined (var hoisted)
2var a = 1;
3
4console.log(b); // ReferenceError (TDZ)
5let b = 2;
6
7const obj = { x: 1 };
8obj.x = 9; // OK — contents are mutable
9obj = {}; // TypeError — binding is constant💡Say this
Default to const, use let only when you must reassign, never use var.
Why interviewers ask this: The fastest way to check whether you understand scope and hoisting.
Common follow-up questions
- →Does const make an object immutable?
- →What is the Temporal Dead Zone?
This is 1 of 118+ questions in the JavaScript Interview Kit
Get every question with detailed answers, follow-ups and real code — plus coding challenges and a last-minute revision sheet. One-time payment, instant access.
⚡ Get the JavaScript Interview Kit → ₹299