JavaScript Const

The const keyword declares variables with constant values that cannot be reassigned. Introduced in ES6 (2015) alongside let, const is used for values that should remain constant throughout the program. Understanding const is crucial: it prevents variable reassignment but doesn't make objects or arrays immutable.

Const Rules

Variables declared with const must be assigned a value when they're declared. You cannot create an uninitialized const variable and assign it later—the declaration and initialization must happen in a single statement. Attempting to declare "const x;" without a value will immediately throw a syntax error. This requirement ensures const variables always have a defined value.

Once assigned, const variables cannot be reassigned to a different value. Any attempt to reassign a const variable will throw a TypeError. For example, "const PI = 3.14159; PI = 3;" would fail. This immutability of the variable binding is the core purpose of const—it communicates that this variable's reference will never change, making code more predictable and easier to understand.

Like let, const has block scope, meaning const variables only exist within the block where they're declared. They follow the same scoping rules as let: confined to their block, not accessible outside it, and subject to the temporal dead zone. This predictable scoping behavior makes const variables local and prevents global namespace pollution.

It's crucial to understand that const does not make the value immutable—it only makes the variable binding (the reference) immutable. For primitive values (numbers, strings, booleans), this distinction doesn't matter since primitives are immutable by nature. However, for objects and arrays, const only prevents reassigning the variable to a different object or array—you can still modify the contents of the object or array itself.

Modern JavaScript best practice is to use const by default and only use let when you specifically need to reassign a variable's value. This "const by default" approach makes code more readable and predictable because anyone reading the code can immediately see which values are intended to remain constant. It also catches accidental reassignments that could be bugs. Reserve let for counters, accumulators, and other values that genuinely need to change.

// Must assign when declaring
const PI = 3.14159;

// Cannot reassign
// PI = 3.14; // Error!

// Block scope
{
  const x = 2;
}
// x cannot be used here

// Must declare with value
// const y; // Error!
// y = 5;

Const with Objects and Arrays

When you declare an object or array with const, the const keyword prevents reassignment of the variable itself—you cannot make the variable point to a different object or array. However, it does NOT make the object or array immutable. You can still modify the properties of const objects and the elements of const arrays. This is a common source of confusion for JavaScript beginners.

You can freely change properties of const objects. Add new properties, modify existing ones, or delete properties—all of these operations work fine because you're modifying the object's contents, not reassigning the variable. The variable still points to the same object in memory, just with different internal values. The const only protects the binding, not the object's properties.

Similarly, you can modify const arrays by adding elements, removing elements, changing existing elements, or using array methods like push, pop, splice, or sort. These operations modify the array's contents but don't reassign the variable. The variable still points to the same array in memory. This allows you to use const for arrays that will be modified, as long as you don't need to replace the entire array.

What you cannot do is reassign a const object or array to a completely different object or array. Attempting "const person = {}; person = {name: 'John'};" will throw an error. The second statement tries to reassign the person variable to a new object, which violates the const constraint. Similarly, you cannot reassign a const array to a new array.

If you need true immutability for objects or arrays, const alone won't provide it. You need additional techniques like Object.freeze() for shallow immutability, immutability libraries like Immer or Immutable.js, or functional programming patterns. But for most use cases, const provides sufficient protection by preventing accidental reassignment while allowing intentional modifications to object and array contents.

// const object - can change properties
const person = {
  name: "John",
  age: 30
};

person.age = 31; // OK
person.city = "New York"; // OK

// person = {}; // Error! Cannot reassign

// const array - can modify
const colors = ["red", "green"];
colors.push("blue"); // OK
colors[0] = "yellow"; // OK

// colors = []; // Error! Cannot reassign