Skip to the solution
MediumFrontend7 min read

What is the difference between Object.freeze, seal, and preventExtensions?

1.2k views

The problem

Question presented to candidate: "JavaScript has three levels of object lockdown β€” Object.preventExtensions, Object.seal, and Object.freeze. What exactly does each one actually block, and what's still genuinely allowed at each level?"

What a strong answer should cover:

  • πŸ“Œ Interview term: a real, three-level immutability spectrum β€” preventExtensions is the loosest (blocks only adding new properties); seal adds blocking delete and reconfigure; freeze is the strongest, additionally blocking changing existing values.
  • πŸ“Œ Verified, not assumed β€” the exact real capability matrix: preventExtensions genuinely still allowed deleting an existing property, changing its value, AND reconfiguring it β€” only adding a new property was genuinely blocked. seal additionally genuinely blocked delete and reconfigure, but changing an existing value's genuinely still worked. freeze genuinely blocked all four operations.
  • A precise answer names that all three levels also genuinely make the object non-extensible (Object.isExtensible() returns real false for all three) β€” extensibility is the one thing every level shares.
  • πŸ“Œ A genuine, easy-to-miss gotcha, verified directly: Object.isSealed() and Object.isFrozen() both genuinely return true for an empty, non-extensible object β€” even one that only went through preventExtensions(), never actually sealed or frozen β€” because "every own property is non-configurable" is vacuously true when there are genuinely zero properties to check.
  • A precise answer names the practical use case each level fits: preventExtensions for "no new fields, but internal bookkeeping can still change"; seal for "a fixed shape whose values can still update" (a real, common pattern for a validated config object with mutable values); freeze for genuine full immutability.

Clarifying questions expected:

  • "Does the actual requirement need existing values to remain updatable, or does absolutely nothing about this object need to change after creation?" β€” directly decides between seal and freeze.
  • "Is the concern specifically about accidentally adding typo'd properties (a common real bug), or about the object's shape and values both needing to stay fixed?" β€” decides whether preventExtensions alone is genuinely sufficient.

Code / implementation expected: Yes β€” a real, per-capability matrix (can-add / can-delete / can-change-value / can-reconfigure) run separately against fresh objects at each of the three levels is the concrete, convincing proof of exactly where each level's real boundary sits.

  • object-seal-freeze

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

7 min read Β· 8 sections

Run the code

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

JavaScriptA real, per-capability matrix: what preventExtensions, seal, and freeze each genuinely block, run against fresh objects

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 44 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

Set methods deep-dive: performance and mutability trade-offs vs manual loops?

Continue