#87: Changing a property of a const object
Topic: objects · Difficulty: ★
const obj = { count: 1 };
obj.count = 2;
console.log(obj.count);Output
2
Why
const only freezes the BINDING — the guarantee that the variable obj can never be made to point at a different value with obj = something. It says nothing about the value itself being immutable. obj still points at the same object the whole time; only its count property is being changed, which is a mutation of the object's contents, not a reassignment of the variable. So this compiles and runs fine, printing 2. To actually prevent an object's properties from changing, you need Object.freeze(obj), which is a separate mechanism entirely from const.