Skip to solution
mediumFrontend

What is the difference between Object.freeze and const?

514 views
01

Understand 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
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every behavior below was actually run — a genuine mutable const object, a genuine froze

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

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

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 87 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track