Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

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]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I declared an object with const. Why can I still change one of its properties? I thought const meant the value could not change.

Geez

const applies to the binding—the name that refers to the object—not to every property inside the object. JavaScript allows you to mutate an object held by a const binding, while disallowing assignment of a different object to that same binding.[S1]

For example, the property update below is allowed. The reassignment is deliberately commented out because it is invalid:

const settings = { theme: "light" };

settings.theme = "dark"; // Allowed: changes a property

// Invalid: attempts to reassign the const binding
// settings = { theme: "dark" };

Follow-up question

So does const make the object shallowly immutable, or is it only stopping reassignment? What would deep immutability mean here?

Geez

It stops reassignment of the binding; it does not, by itself, make the object shallowly or deeply immutable. The example above changes a property on the object, and that is compatible with a const declaration.[S1]

“Deeply immutable” describes a stronger property: changes are prevented not only to an outer object’s properties but also to values nested inside it. const alone does not establish that. The key distinction is that const fixes the binding, not the contents of the referenced object.

Follow-up question

Can you show both cases together, including an example of what I should not do, so I can explain this to a teammate?

Geez

Here is a compact example. The property change is allowed; the reassignment is labeled invalid and left commented out:

const profile = { name: "Rae" };

profile.name = "Rae Example"; // Allowed: property mutation

// Invalid: cannot assign a different object to this const binding
// profile = { name: "New profile" };

The first operation changes a property of the object referred to by profile. The second tries to assign a different object to the profile binding. These examples show what const allows or disallows; they do not demonstrate a deep-immutability mechanism.[S1]

Follow-up question

What quick rule can I use when reviewing code, and how should I describe the limitation accurately?

Geez

Ask two separate questions: “Is this code assigning a new value to the variable?” and “Is it changing a property of the object?” With const, reassignment of the binding is disallowed, but changing object properties is allowed.[S1] A concise description is: “const prevents rebinding this variable; it does not make the referenced object deeply immutable.” Avoid saying that const freezes the object or all of its nested values.

A practical checklist

  • Find the const declaration 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 const alone 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 const guarantees.

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.

  1. [S1] const — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work