In most modern codebases, const accounts for the vast majority of declarations. That is not a stylistic tic, it is the default setting recommended by every serious style guide.
The name is misleading, though. const does not create a constant in the mathematical sense, and clearing up that misunderstanding is the first thing to do.
Definition
const declares a name bound to a value, and forbids binding that name to another value later. The scope is the block, exactly as with let, and the value must be supplied on the declaration line itself.
const VAT = 0.2;
const grossPrice = (net) => net * (1 + VAT);
console.log(grossPrice(100)); // 120
VAT = 0.1; // TypeError: Assignment to constant variable.The reassignment attempt fails at runtime, with an unambiguous message. It is a cheap guardrail: it catches the accidental change of a value that was never meant to move, often hundreds of lines away from its declaration.
What const does not protect
The lock applies to the binding, not to the contents. When the value is an Object or an Array, the name stays attached to the same object, but the object itself can change as much as it likes.
const cart = { total: 0 };
cart.total = 49; // allowed: the object changes, the binding does not
console.log(cart.total); // 49
cart = { total: 0 }; // TypeError: the binding is frozenThis is the point that surprises people most often, and it makes sense once the distinction is drawn: the variable does not hold the object, it holds a reference to it. const freezes the reference.
To block changes to the contents as well, Object.freeze() exists, but it only freezes the top level: a nested object stays editable. Inside an ES module, always in strict mode, writing to a frozen object raises an error; elsewhere the assignment is silently ignored.
const, let or var
| Keyword | Scope | Redeclaration | Reassignment |
|---|---|---|---|
const | The block | Refused | Refused |
let | The block | Refused | Allowed |
var | The function | Allowed | Allowed |
The procedure fits in two lines: write const everywhere, and switch it to let the day the engine refuses a reassignment you genuinely need. var no longer enters the decision.
Frequently asked questions
Does const make a value immutable?
No, and this is the most common confusion. An array declared with const happily accepts a push or an in-place sort. Only the link between the name and the value is locked, which is a useful guarantee but a far weaker one than immutability.
Can const be used in a loop?
In a for...of loop, yes, and it is the recommended form: every turn creates a fresh binding, which has no reason to change during that turn. In a classic counter loop, no, since the counter is incremented on every pass.
Is const faster than let?
Not measurably in real code: modern engines optimize both the same way. The gain sits elsewhere, in reading. A file where only three lines carry let announces at a glance the only three places where something moves. Our JavaScript course starts from these declaration habits to build codebases that stay readable at several thousand lines.