Updating Property Values
You'll learn how to change the values of properties that already exist in an object — and why objects are mutable by reference.
The Core Idea
Once you've added a property to an object, you can reassign its value at any time using the same syntax you used to create it. You simply assign a new value to that property key, and JavaScript replaces the old value with the new one.
const user = {
name: "Alice",
score: 10
};
// Update the score
user.score = 25;
console.log(user.score); // 25
// You can use bracket notation too
user["name"] = "Alicia";
console.log(user.name); // "Alicia"
Objects Are Mutable by Reference
Here's the important part: even if you declare an object with const, you can still update its properties. The const prevents you from reassigning the variable itself, but the object it points to remains mutable.
const car = { color: "red" };
car.color = "blue"; // Allowed ✓
console.log(car.color); // "blue"
car = { color: "green" }; // Error! ✗ Cannot reassign the variable
Think of it like a house with a permanent address (const). You can redecorate the rooms inside (update properties), but you can't tear down the house and build a new one at that address (reassign the variable).
When multiple variables reference the same object, updating a property through one variable affects all references:
const obj1 = { count: 5 };
const obj2 = obj1; // Same object, different name
obj2.count = 10;
console.log(obj1.count); // 10 (changed!)
Key Takeaway: You can update object properties freely even in const objects because objects are mutable — const only prevents variable reassignment, not property mutation.