Const and Object Mutability
What you'll learn: How const stops you from reassigning a variable, but still lets you change the contents of objects and arrays.
The Confusion with Const
When you declare a variable with const, JavaScript prevents you from reassigning it to a different value. But here's the surprise: if that const variable holds an object (or array), you can still mutate (change) the object's properties!
const person = { name: "Alice", age: 25 };
// ❌ This fails — reassignment is blocked
person = { name: "Bob", age: 30 }; // TypeError
// ✅ This works — mutation is allowed
person.age = 26;
person.city = "Paris";
console.log(person); // { name: "Alice", age: 26, city: "Paris" }
Why This Happens
Remember: const protects the reference (the arrow pointing to the object in memory), not the object itself. Think of it like a locked address book entry — you can't change which house the entry points to, but you can repaint that house, add rooms, change the furniture, etc.
const colors = ["red", "blue"];
colors.push("green"); // ✅ Works — mutating the array
colors[0] = "yellow"; // ✅ Works — changing an element
colors = ["purple"]; // ❌ Fails — reassignment blocked
This is consistent with what you learned earlier: objects store references, not values, and const prevents reassignment, not mutation.
Key Takeaway: const locks the variable binding (prevents reassignment) but does not make objects immutable — you can still modify their properties and contents.