How are `const` objects different from deeply immutable objects?
Quick answer
const prevents reassignment of the variable binding, not changes to the object referred to by that binding. You can update an object’s properties while keeping the same binding, but assigning a different object to that binding is not allowed. Deep immutability is a separate idea: it means changes to the object and its nested values are prevented, which const alone does not provide.[S1]
The question, the details, the next step.
A practical checklist
Find the
constdeclaration and identify the variable name whose binding is fixed.Check whether each later operation assigns a new value to that variable or changes a property on the object.
Remember that
constalone does not prevent object-property mutation.Label reassignment examples as invalid and keep them commented out in a demonstration.
Use “non-reassignable binding” rather than “immutable object” to describe what
constguarantees.
A few more useful answers
Can I change a property on an object declared with `const`?
Yes. const does not prevent changing the object’s properties; it prevents reassigning the binding to a different value.[S1]
Does `const` recursively freeze nested objects?
No. A const declaration alone does not make an object deeply immutable. It fixes the binding, not the referenced object’s contents.[S1]
What is the shortest accurate explanation of `const` for objects?
The variable binding cannot be reassigned, but the object it refers to can still be mutated.[S1]
Sources
References used for this guide.
- [S1] const — JavaScript | MDN developer.mozilla.org · Checked
