Compile Ready
Module 6 · Objects & Prototypes

Object.seal

Intermediate8m read5m practice13m total
Object.sealpreventExtensionsDescriptorsObject Shape

Introduction

Object.seal(obj) locks an object's top-level shape without necessarily locking its existing values. After sealing, you cannot add properties, delete properties, or reconfigure existing properties, but writable data properties can still be updated.

Think of seal as same keys, values may change. That makes it different from Object.freeze, which also makes data properties non-writable.

Why This Matters

Seal, freeze, and preventExtensions are easy to mix up. Interviewers often ask for a precise comparison because the difference maps directly to property descriptors: extensibility, configurability, and writability.

Theory

What Object.seal does

Sealing an object:

  1. Prevents extensions, so no new own properties can be added.
  2. Marks existing own properties as configurable: false, so they cannot be deleted or reconfigured.
  3. Leaves writable unchanged for data properties. If a property was writable before sealing, its value can still change.

Seal vs freeze

Object.freeze is stricter. Freeze includes everything seal does and additionally marks existing data properties as non-writable. Seal locks the set of properties; freeze locks both the set and the top-level values.

Seal vs preventExtensions

Object.preventExtensions only prevents adding new properties. Existing configurable properties can still be deleted. Object.seal goes further by making existing properties non-configurable.

Shallow operation

Like freeze, seal is shallow. If a sealed object points to a nested object, that nested object can still gain properties, lose properties, or be mutated unless it is separately sealed or frozen.

Reliable checks

Use Object.isSealed(obj) to test the target object. Use Reflect.defineProperty and Reflect.deleteProperty in examples when you want boolean results instead of strict-mode-dependent exceptions.

Visual Diagrams

preventExtensions vs seal vs freeze
Operation                Add props   Delete props   Change writable values
-----------------------  ---------   ------------   ----------------------
preventExtensions        no          maybe          yes
seal                     no          no             yes, if writable
freeze                   no          no             no, for data props

Each operation adds another descriptor-level restriction.

Code Examples

A sealed object can still update writable values

The shape is locked, but normal writable data properties can still receive new values.

Loading…

preventExtensions is weaker than seal

A non-extensible object cannot gain properties, but configurable existing properties can still be deleted.

Loading…

Playground

Loading editor…
Console

Press Run to execute the code and see output here.

Output Prediction

Predict the output #1

javascript
1const settings = { mode: 'dev' };
2
3Object.seal(settings);
4settings.mode = 'prod';
5
6const added = Reflect.defineProperty(settings, 'debug', {
7 value: true
8});
9
10console.log(settings.mode);
11console.log(added);
12console.log(Object.keys(settings).join(','));

Predict the output #2

javascript
1const obj = { a: 1 };
2
3Object.preventExtensions(obj);
4
5const added = Reflect.defineProperty(obj, 'b', {
6 value: 2
7});
8const deleted = Reflect.deleteProperty(obj, 'a');
9
10console.log(added);
11console.log(deleted);
12console.log(Object.keys(obj).length);

Coding Exercises

Lock an API response shape

Easy

Implement sealUser(user) so callers can update existing top-level fields but cannot add or remove top-level fields. Return the same object.

Interview Questions

1How is `Object.seal` different from `Object.freeze`?

Both prevent extensions and make existing own properties non-configurable. Object.freeze additionally makes existing data properties non-writable. A sealed object's writable properties can still change; a frozen object's data properties cannot.

Asked at:GoogleAmazon

Follow-ups

  • Where does `Object.preventExtensions` fit in?
  • Are these operations deep or shallow?
2Can you delete a property from a sealed object?

No. Sealing marks existing own properties as non-configurable, and non-configurable properties cannot be deleted. Reflect.deleteProperty returns false; delete can throw in strict mode.

Asked at:MicrosoftMeta

Quiz

1. After `Object.seal(obj)`, which operation can still work for a writable data property?

2. Which operation is the weakest shape restriction?

Summary

  • `Object.seal` prevents adding new own properties and deleting existing own properties.
  • Sealed properties become non-configurable, but writable data properties can still change value.
  • `Object.freeze` is stricter because it also makes data properties non-writable.
  • `Object.preventExtensions` is weaker because existing configurable properties can still be deleted.

Cheat Sheet

preventExtensions: cannot add new properties.

seal: cannot add/delete/reconfigure top-level properties; writable values may change.

freeze: seal + data properties become non-writable.

Checks: Object.isExtensible, Object.isSealed, Object.isFrozen.

All are shallow.