const in JavaScript: the default declaration, and what it does not protect

const freezes the link between a name and a value, without making the object itself immutable: the declaration to reach for by default in modern JavaScript.
3 min read
Believemy logo

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.

JAVASCRIPT
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.

JAVASCRIPT
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 frozen

This 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.

Good to know

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

KeywordScopeRedeclarationReassignment
constThe blockRefusedRefused
letThe blockRefusedAllowed
varThe functionAllowedAllowed

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

Question

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.


Question

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.


Question

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.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.