Compare var, let and const. Cover scope, hoisting, redeclaration and reassignment, and say which you would use by default.
Show hints
- Think about what happens inside a for loop with setTimeout.
Show the explanation
Model answer.
- Scope:
varis function-scoped;letandconstare block-scoped (they live inside the nearest{ }). - Hoisting: all three are hoisted, but a
varstarts asundefined, whilelet/constsit in the temporal dead zone: touching them before the declaration line throws aReferenceError. - Redeclaration:
var xtwice in one scope is allowed;let/consttwice is aSyntaxError. - Reassignment:
constcannot be reassigned, but the object it points to can still change.
const user = { name: 'Asha' };
user.name = 'Ravi'; // fine: the object changed, the binding did not
// user = {}; // TypeError: Assignment to constant variableA classic bug: for (var i = 0; i < 3; i++) setTimeout(() => console.log(i)) prints 3 3 3, because there is one shared i. With let each iteration gets its own i and it prints 0 1 2.
Default: use const, switch to let only when the value must change, and avoid var.
Original UniCircle question. Last reviewed 22 Oct 2026.
