JavaScript
Programming
Web Development
Coding
Computer Science

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.

javascript
1const taxRate = 0.13;
2console.log(taxRate);
3
4// taxRate = 0.15; // TypeError

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.

javascript
1const settings = {
2  theme: "light",
3  notifications: true
4};
5
6settings.theme = "dark";
7console.log(settings.theme); // dark
8
9// settings = {}; // TypeError

The object can still change because the reference is constant, not the object state. The same rule applies to arrays.

javascript
const numbers = [1, 2, 3];
numbers.push(4);
console.log(numbers); // [1, 2, 3, 4]

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.

javascript
1const config = Object.freeze({
2  apiUrl: "https://example.com",
3  timeoutMs: 3000
4});
5
6// config.timeoutMs = 5000; // Fails in strict mode
7console.log(config.timeoutMs);

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 const when the binding should not change
  • use let only when reassignment is required
  • avoid var in 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.

javascript
1const items = ["a", "b", "c"];
2let index = 0;
3
4while (index < items.length) {
5  console.log(items[index]);
6  index += 1;
7}

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.

javascript
const MAX_RETRIES = 3;
const API_BASE_URL = "https://api.example.com";

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 const makes objects and arrays deeply immutable.
  • Using let everywhere even when reassignment never happens.
  • Treating uppercase names as magical language-level constants instead of conventions.
  • Forgetting that const declarations must be initialized immediately.
  • Reaching for runtime freezing when the real need is simply clearer program structure.

Summary

  • JavaScript supports constant bindings with the const keyword.
  • 'const prevents reassignment of the variable name, not deep mutation of object contents.'
  • Objects and arrays declared with const can still be modified unless extra measures are taken.
  • 'Object.freeze adds shallow immutability for object properties.'
  • In modern JavaScript, const is usually the default choice unless reassignment is actually needed.

Course illustration
Course illustration

All Rights Reserved.