What is the difference between Object.freeze, Object.seal, and Object.preventExtensions?
The short answer
All three restrict what you can do to an object, but at different levels. Object.preventExtensions stops you from adding new properties. Object.seal does that plus prevents deleting properties. Object.freeze does all of that plus prevents changing existing property values. Freeze is the most restrictive.
Object.preventExtensions
You cannot add new properties, but you can modify and delete existing ones:
const obj = { name: 'John', age: 30 };Object.preventExtensions(obj);obj.name = 'Jane'; // works: modifies an existing propertydelete obj.age; // works: deletes an existing propertyobj.email = 'john@example.com'; // silently fails (throws in strict mode)Object.seal
You cannot add or delete properties, but you can still change existing values:
const obj = { name: 'John', age: 30 };Object.seal(obj);obj.name = 'Jane'; // works: modifies an existing propertydelete obj.age; // ignored (TypeError in strict mode)obj.email = 'john@example.com'; // ignored (TypeError in strict mode)Object.freeze
You cannot add, delete, or change properties. The object becomes completely immutable:
const obj = { name: 'John', age: 30 };Object.freeze(obj);obj.name = 'Jane'; // ignored (TypeError in strict mode)delete obj.age; // ignored (TypeError in strict mode)obj.email = 'john@example.com'; // ignored (TypeError in strict mode)Comparison table
| Add properties | Delete properties | Modify values | |
|---|---|---|---|
preventExtensions | No | Yes | Yes |
seal | No | No | Yes |
freeze | No | No | No |
Important: shallow only
All three methods are shallow. They do not affect nested objects:
const obj = { name: 'John', address: { city: 'NYC' } };Object.freeze(obj);obj.name = 'Jane'; // fails: obj is frozenobj.address.city = 'Boston'; // works: the nested object is not frozenTo deep freeze, you would need to recursively freeze all nested objects.
Checking the state
Object.isExtensible(obj); // false if preventExtensions/seal/freezeObject.isSealed(obj); // true if sealed or frozenObject.isFrozen(obj); // true if frozenInterview Tip
Use the comparison table to make the differences clear. Mention that all three are shallow. This is the most common follow-up question. If you can explain a use case (like freezing configuration objects to prevent accidental modification), that shows practical application.
Why interviewers ask this
This tests your knowledge of JavaScript's object immutability tools. Interviewers want to see if you know the differences and when you would use each one. Understanding these methods shows you think about data integrity in your applications.