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

JavaScript

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 — modifying existing property
delete obj.age; // works — deleting existing property
obj.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 — modifying existing property
delete obj.age; // silently fails — cannot delete
obj.email = 'john@example.com'; // silently fails — cannot add

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'; // silently fails — cannot modify
delete obj.age; // silently fails — cannot delete
obj.email = 'john@example.com'; // silently fails — cannot add

Comparison table

Add propertiesDelete propertiesModify values
preventExtensionsNoYesYes
sealNoNoYes
freezeNoNoNo

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 — frozen
obj.address.city = 'Boston'; // works! — nested object is not frozen

To deep freeze, you would need to recursively freeze all nested objects.

Checking the state

Object.isExtensible(obj); // false if preventExtensions/seal/freeze
Object.isSealed(obj); // true if sealed or frozen
Object.isFrozen(obj); // true if frozen

Interview 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.