Const Prevents Reassignment, Not Mutation
What you'll learn: Why const stops you from pointing to a new object but doesn't freeze the object itself.
The Core Idea
When you declare a variable with const, JavaScript prevents you from reassigning that variable to a different value. However, if the variable holds a reference to an object (or array), const does not prevent you from mutating (changing) the contents of that object.
Think of it like a locked address label on a box. The const keyword locks the label in place—you can't stick a new label pointing to a different box—but you can still open the box and rearrange, add, or remove what's inside.
How It Works
const person = { name: "Alice" };
// ✅ This works — mutating the object
person.name = "Bob";
person.age = 30;
console.log(person); // { name: "Bob", age: 30 }
// ❌ This fails — trying to reassign
person = { name: "Charlie" }; // TypeError: Assignment to constant variable
The variable person always points to the same object reference. But the properties inside that object can change freely.
The same applies to arrays:
const numbers = [1, 2, 3];
numbers.push(4); // ✅ Mutating the array
console.log(numbers); // [1, 2, 3, 4]
numbers = [5, 6]; // ❌ TypeError
Why This Matters
const protects the reference, not the data. You can't accidentally point the variable somewhere else, but you're still responsible for what happens to the object itself.
Key Takeaway: const prevents reassignment of the variable but allows mutation of the object or array it references.