let vs var vs const
By FrontendPro Editorial Team Updated 9/26/2026
Answer
var | let | const | |
|---|---|---|---|
| Scope | Function | Block | Block |
| Redeclare in same scope | Yes | No | No |
| Reassign | Yes | Yes | No |
| Read before declaration | undefined | ReferenceError | ReferenceError |
| Must initialise | No | No | Yes |
Creates a window property | Yes (at top level) | No | No |
function demo() {
if (true) {
var a = 1;
let b = 2;
}
a; // 1 — var ignores the block
b; // ReferenceError
}
const does not mean immutable
It prevents reassigning the binding, not changing the value:
const user = { name: "Mai" };
user.name = "Nam"; // fine — the object is mutable
user = {}; // TypeError — cannot rebind
Use Object.freeze if you need the contents protected too.
The loop case
let creates a fresh binding per iteration; var shares one:
for (var i = 0; i < 3; i++) setTimeout(() => console.log(i)); // 3, 3, 3
for (let i = 0; i < 3; i++) setTimeout(() => console.log(i)); // 0, 1, 2
This alone justifies dropping var.
Hoisting is not the difference people think
All three are hoisted. var is also initialised to undefined, so reading it early is legal and silently useless. let and const stay uninitialised until their declaration line — the temporal dead zone — so reading early throws at the exact point of the mistake.
What to use
const by default, let when you genuinely reassign, var never. Starting with const also makes reassignment visible in review: if a variable is let, the reader knows to look for where it changes.
