What is the difference between var, let and const?
Quick Answer
var is function-scoped, hoisted and initialised to undefined, and can be redeclared. let and const are block-scoped and hoisted but not initialised (the “temporal dead zone” until the declaration). const also can't be reassigned — though a const object's properties can still change. Use const by default, let when you must reassign, and avoid var.
var is function-scoped and gets hoisted to the top of the function as undefined, so you can reference it before the line it's declared on. let and const are block-scoped — they only exist inside the nearest braces — and although they're hoisted, you can't use them before the declaration; that gap is the temporal dead zone. const on top of that can't be reassigned, but that only locks the binding, not the contents, so you can still push to a const array. My rule is const everywhere, let when I genuinely need to reassign, and never var.
| var | let | const | |
|---|---|---|---|
| Scope | Function | Block | Block |
| Hoisting | Yes → undefined | Yes → TDZ | Yes → TDZ |
| Reassign | Yes | Yes | No |
| Redeclare in same scope | Yes | No | No |
const prevents reassigning the binding, not mutating the value: const a = []; a.push(1) is fine, a = [2] throws. Block scoping is why let fixes the classic for-loop closure bug.