
Short answer: use const by default, let when the variable has to be reassigned, and avoid var in new code. let and const are block-scoped and can’t be used before their declaration. var is function-scoped and hoisted, which causes bugs that the newer keywords were made to prevent.
All three are reserved words for declaring variables. Here’s how they differ, with code you can paste into the console.
The differences at a glance
- Scope:
varis scoped to the function.letandconstare scoped to the nearest block: any{ }, includingifand loops. - Hoisting: all three are hoisted, but
varstarts asundefined, whileletandconstthrow aReferenceErroruntil the declaration line runs. - Redeclaring:
varlets you declare the same name twice in a scope.letandconstthrow aSyntaxError. - Reassigning:
varandletcan be reassigned.constcan’t. - Global object: a top-level
varin a classic script becomes a property ofwindow.letandconstdon’t.
Scope: function vs block
A var declared inside an if block leaks out to the whole function. let and const stay inside the block where you declared them, which is what you’d expect from most other languages.
function scopeDemo() {
if (true) {
var a = 1;
let b = 2;
const c = 3;
}
console.log(a); // 1: var ignores the block
console.log(b); // ReferenceError: b is not defined
}
scopeDemo();Hoisting and the temporal dead zone
Declarations are moved to the top of their scope before the code runs. A hoisted var is initialized to undefined, so reading it early silently gives you undefined. A hoisted let or const isn’t initialized: the time between the start of the scope and the declaration is called the temporal dead zone, and any access there throws. That error is a feature: it points at the bug instead of hiding it.
console.log(x); // undefined
var x = 5;
console.log(y); // ReferenceError: Cannot access 'y' before initialization
let y = 5;The classic loop bug
This is the bug that made let necessary. With var, every function created in the loop shares one variable, which has already reached 3 by the time they run. let creates a new binding for each iteration, so each function keeps its own value.
const withVar = [];
for (var i = 0; i < 3; i++) {
withVar.push(() => i);
}
console.log(withVar.map((fn) => fn())); // [3, 3, 3]
const withLet = [];
for (let j = 0; j < 3; j++) {
withLet.push(() => j);
}
console.log(withLet.map((fn) => fn())); // [0, 1, 2]const means "can't reassign", not "can't change"
const locks the variable, not the value. You can’t point a const at something else, but if it holds an object or an array, you can still change what’s inside. To make an object read-only, use Object.freeze(). It’s shallow: nested objects stay writable. In normal scripts, writes to a frozen object are silently ignored; in strict mode and in modules, they throw a TypeError.
const user = { name: 'Ana' };
user.name = 'Marko'; // fine: the object changes
user = {}; // TypeError: Assignment to constant variable.
const list = [1, 2];
list.push(3); // fine: [1, 2, 3]
const settings = Object.freeze({ theme: 'dark' });
settings.theme = 'light'; // ignored (TypeError in strict mode)
console.log(settings.theme); // 'dark'
const total; // SyntaxError: Missing initializer in const declarationconst in loops
Use const in for...of and for...in loops: each iteration gets a fresh variable, so nothing is reassigned. In a classic for loop, the counter is incremented, so it needs let.
for (const item of ['a', 'b']) {
console.log(item); // works
}
for (const i = 0; i < 2; i++) {
// TypeError: Assignment to constant variable.
}Which one should you use?
- Start with
const. Most variables are never reassigned. When a reader seesconst, they know the name means the same thing all the way down. - Switch to
letwhen you actually reassign: counters, accumulators, a value that changes in a loop or atry/catch. - Don’t use
varin new code. You’ll still read it in older code and tutorials, so it’s worth knowing how it behaves.
Linters can enforce this. ESLint’s prefer-const and no-var rules flag a let that’s never reassigned and any var.
Not sure what a variable holds at runtime? Check it with typeof. And if you’re converting user input before storing it, see how to convert a string to a number.
FAQ
Is let faster than var?
Not in any way you’ll notice. Pick the keyword for what it says about the variable, not for speed.
Is var deprecated?
No. var still works everywhere and will keep working, because removing it would break old websites. It’s just no longer the recommended choice.
Can I declare a variable without var, let or const?
In sloppy mode, assigning to an undeclared name creates a global variable by accident. In strict mode and in modules, it throws a ReferenceError. Always declare your variables.
Why does const allow changing an array?
Because const protects the binding, not the value. list.push(3) changes the array the variable points to, but the variable still points to the same array.
Should I use let or const in a for loop?
let for a counter that’s incremented, const in for...of and for...in, where each iteration gets a new variable.
