`let` vs `const` vs `var`
let, const, and var all declare variables, but they differ in scope, in what happens if you read them too early, and in whether the name can be reassigned or redeclared. Modern code uses const by default and let when the value must change.
Detailed Explanation
Until ES2015, var was the only option, and it had one behaviour that caused endless bugs: it is scoped to the enclosing function, not to the enclosing block. A var inside an if block or a for loop is visible throughout the whole function, including before the line that declares it. Developers worked around this by wrapping code in immediately invoked functions purely to get a private scope. let and const were added to make blocks real scopes, so the workaround is no longer needed.
Here is the whole difference in one table.
| Behaviour | var | let | const |
|---|---|---|---|
| Scope | nearest function | nearest block | nearest block |
| Before its line | undefined | throws ReferenceError | throws ReferenceError |
| Reassignment | allowed | allowed | TypeError |
| Redeclaration in same scope | allowed | SyntaxError | SyntaxError |
| Initializer required | no | no | yes |
Creates a property on globalThis | yes, at script top level | no | no |
| Binding per loop iteration | one, shared | one per iteration | one per iteration |
Two rows deserve expansion. The "before its line" row is the Temporal Dead Zone: let and const bindings are created when the block is entered but stay uninitialized until their declaration executes, and touching them in between throws. var instead starts life as undefined, which means a use-before-declare mistake produces a confusing value rather than an error.
The "binding per iteration" row is why closures behave differently in loops. A for (let i = 0; ...) loop makes a fresh i on every pass, so a callback created inside the loop captures that iteration's value. With var there is one i for the entire function, so every callback sees the final value. This is one of the most common interview questions in the language.
const is the one people misread. It means the binding cannot be pointed at a different value. It says nothing about the value's contents. A const array can be pushed to, sorted, and emptied. If you want the contents protected, Object.freeze is the tool, and it is shallow.
The practical rule: reach for const first, because a name that never changes is one less thing to track while reading. Switch to let when you genuinely reassign — a loop counter, an accumulator, a value assigned in two branches. Use var only when maintaining old code that depends on its hoisting, or when writing a quick expression in a browser console, where let cannot be redeclared and var can.
There is no runtime cost to choosing let or const over var. Engines handle all three efficiently, and block scoping often helps them reason about lifetimes.
Code Examples
Scope: var leaks out of the block, let does not.
// Scope: var leaks out of the block, let does not.
function check(flag) {
if (flag) {
var loud = "escapes the block";
let quiet = "stays in the block";
console.log(quiet); // => stays in the block
}
console.log(loud); // => escapes the block (var is function-scoped)
// console.log(quiet); // ReferenceError: quiet is not defined
}
check(true);Loop bindings: the single most visible difference.
// Loop bindings: the single most visible difference.
const withVar = [];
for (var i = 0; i < 3; i++) withVar.push(() => i);
console.log(withVar.map((f) => f())); // => [3, 3, 3] one shared i, left at 3
const withLet = [];
for (let j = 0; j < 3; j++) withLet.push(() => j);
console.log(withLet.map((f) => f())); // => [0, 1, 2] a fresh j per iterationconst locks the binding, not the object.
// const locks the binding, not the object.
const cache = new Map();
cache.set("user:1", { name: "Ana" }); // mutating the Map is allowed
console.log(cache.size); // => 1
// cache = new Map(); // TypeError: Assignment to constant variable.
let attempts = 0;
attempts += 1; // let exists for exactly this case
console.log(attempts); // => 1Common Gotchas
Reassigning a `const` throws a `TypeError`
the message is `Assignment to constant variable`. Note that this is a runtime error, while *redeclaring* `let x` twice in one scope is a `SyntaxError` caught before any code runs.
`var` in a loop plus a callback is a bug factory
timers, event handlers, and promise callbacks created inside a `var` loop all share one binding and observe its final value. Change `var` to `let` and the problem disappears.
Top-level `var` and `let` differ on the global object
in a classic browser script, `var appName` becomes `globalThis.appName`, while `let appName` does not. Inside an ES module or a Node CommonJS file, neither one touches the global object.
`const` in a `for...of` loop works, but not in a classic `for`
`for (const item of items)` is fine because each iteration gets a new binding, whereas `for (const i = 0; i < 3; i++)` throws on the first `i++`.