Are there constants in JavaScript?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
JavaScript does have constants, but the answer is slightly more subtle than just "use const." The const keyword creates a binding that cannot be reassigned, which is perfect for many values, but it does not make objects and arrays deeply immutable by itself.
What const Actually Guarantees
A variable declared with const must be initialized immediately and cannot later be assigned to a different value.
This makes const ideal when the reference should remain stable. The runtime enforces that rule, so accidental reassignment becomes an error instead of a hidden bug.
const Does Not Freeze Objects
The most common misunderstanding is thinking that const makes the contents immutable. It does not. It only protects the variable binding.
The object can still change because the reference is constant, not the object state. The same rule applies to arrays.
That is why const is often described as preventing reassignment rather than creating true immutable data.
Use Object.freeze When You Need More Protection
If you want to prevent modifications to an object's own properties, Object.freeze is one tool you can use.
Even that is only shallow. Nested objects can still change unless they are frozen too. In practice, many applications rely on discipline, TypeScript types, or immutable data patterns rather than trying to freeze everything at runtime.
Prefer const by Default
In modern JavaScript, a common style is:
- use
constwhen the binding should not change - use
letonly when reassignment is required - avoid
varin new code
That policy reduces accidental mutation and makes code easier to reason about. If a variable is declared with const, readers immediately know the name will not point to a different value later in the block.
Here, items stays stable and index changes. The keywords help communicate that difference.
Naming Conventions Still Matter
Some teams use uppercase names such as MAX_RETRIES or API_BASE_URL for values meant to be treated as constants by convention.
This is only a naming convention, but it is a useful one. It signals that the value is configuration or a fixed domain rule rather than ordinary local state.
Common Pitfalls
- Assuming
constmakes objects and arrays deeply immutable. - Using
leteverywhere even when reassignment never happens. - Treating uppercase names as magical language-level constants instead of conventions.
- Forgetting that
constdeclarations must be initialized immediately. - Reaching for runtime freezing when the real need is simply clearer program structure.
Summary
- JavaScript supports constant bindings with the
constkeyword. - '
constprevents reassignment of the variable name, not deep mutation of object contents.' - Objects and arrays declared with
constcan still be modified unless extra measures are taken. - '
Object.freezeadds shallow immutability for object properties.' - In modern JavaScript,
constis usually the default choice unless reassignment is actually needed.

