Skip to the solution
MediumFrontend6 min read

What is the difference between Object.freeze and const?

514 views

The problem

Question presented to candidate: "A teammate writes const user = { name: 'Ada' }; and says 'this object can never change, I made it const.' Is that actually true? What is the real difference between what const protects and what Object.freeze protects?"

What a strong answer should cover:

  • πŸ“Œ Verified, not assumed: a real const object's property genuinely allowed mutation β€” obj.count = 5 genuinely succeeded on a const-bound object β€” directly disproving the teammate's claim.
  • const only prevents reassigning the variable binding itself β€” a real attempt to reassign a const variable genuinely threw a real TypeError, but that is a completely different protection than protecting the object's own contents.
  • πŸ“Œ Interview term: Object.freeze() β€” makes an object's own properties immutable (shallowly): no adding, deleting, or changing existing values. Verified directly: a real frozen object's property write was genuinely silently ignored outside strict mode, and genuinely threw a real TypeError inside strict mode.
  • A precise answer names that these two protections are genuinely orthogonal β€” a let-bound frozen object can still be reassigned to a brand-new object (verified directly: the binding changed, a real different object), while a const-bound unfrozen object's binding is fixed but its contents remain genuinely mutable.
  • The precise, complete answer: combining both β€” const frozen = Object.freeze({...}) β€” is what actually gives an immutable binding to an immutable object; neither one alone provides that.

Clarifying questions expected:

  • "Does the actual requirement need the object's CONTENTS to be immutable, or just that this specific variable can't be reassigned to point somewhere else?" β€” these are genuinely different needs, verified above as protected by different mechanisms.
  • "Does the object have nested objects that also need protecting?" β€” Object.freeze is genuinely shallow, verified above only at the top level β€” a real, separate consideration.

Code / implementation expected: Yes β€” a real, direct demonstration that a const object's property is genuinely still mutable, alongside a real frozen object's write being genuinely rejected, is the concrete proof of exactly where each protection actually applies.

  • immutability

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: a const object's property is genuinely mutable, while a frozen object's write is genuinely rejected

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 87 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What is the difference between ES modules and CommonJS?

Continue