What is Object.freeze() and how is it different from Object.seal()?

👨‍💻 Frontend Developer 🟠 May come up 🎚️ Medium
#JavaScript #Objects #JS Basics

Brief Answer

Object.freeze() makes an object completely immutable — cannot add, delete or modify properties. Object.seal() allows changing values of existing properties, but prohibits adding new properties and deleting existing ones. Main difference: freeze locks everything, seal locks only structure.


Full Answer

Object.freeze() and Object.seal() are methods for protecting objects from changes. They are similar, but have important differences.

Object.freeze()

Makes object completely immutable:

const obj = { name: 'John', age: 25 };
Object.freeze(obj);
 
// All these operations won't work
obj.name = 'Peter'; // Won't change
obj.city = 'Moscow'; // Won't add
delete obj.age; // Won't delete

Object.seal()

Protects only object structure:

const obj = { name: 'John', age: 25 };
Object.seal(obj);
 
// Can change values
obj.name = 'Peter'; // Will work
 
// Cannot add/delete properties
obj.city = 'Moscow'; // Won't add
delete obj.age; // Won't delete

Main Differences

Object.freeze()

  • Cannot change property values
  • Cannot add properties
  • Cannot delete properties
  • Full lock

Object.seal()

  • Can change property values
  • Cannot add properties
  • Cannot delete properties
  • Partial lock

Simple Examples

When to Use freeze

// Configuration that cannot be changed
const config = { 
  apiEndpoint: 'https://api.example.com',
  timeout: 5000 
};
 
Object.freeze(config);
// Now config is fully protected

When to Use seal

// Object with fixed set of fields
const user = { name: 'John', age: 25 };
Object.seal(user);
 
// Can update user data
user.name = 'Peter';
user.age = 30;
 
// But cannot add new fields
user.email = 'test@example.com'; // Won't work

State Checking

Check if Object is Frozen

const obj = { name: 'John' };
Object.freeze(obj);
console.log(Object.isFrozen(obj)); // true

Check if Object is Sealed

const obj = { name: 'John' };
Object.seal(obj);
console.log(Object.isSealed(obj)); // true

Important Points

1. Only Shallow Protection

const obj = { 
  user: { name: 'John' } 
};
 
Object.freeze(obj);
obj.user.name = 'Peter'; // Will work! Nested object not frozen

2. Cannot Unfreeze

const obj = { name: 'John' };
Object.freeze(obj);
// No way to "unfreeze" object

Common Mistakes

1. Expecting Errors

// ❌ No error in non-strict mode
const obj = { name: 'John' };
Object.freeze(obj);
obj.name = 'Peter'; // Silently won't work
 
// ✅ In strict mode will be error
'use strict';
obj.name = 'Peter'; // TypeError

2. Misunderstanding Nesting

// ❌ Think everything is frozen
const obj = { user: { name: 'John' } };
Object.freeze(obj);
obj.user.name = 'Peter'; // Will work!
 
// ✅ Need to freeze nested objects
Object.freeze(obj.user);
obj.user.name = 'Peter'; // Now won't work

Simple Rules

  1. freeze — when object must be completely immutable
  2. seal — when structure is fixed, but values can be changed
  3. Shallow protection — only first level of object
  4. Cannot undo — after freeze/seal object cannot return to original state
  5. Silent behavior — in non-strict mode changes are just ignored

Understanding difference between freeze and seal helps create more reliable code and prevent unwanted data changes.


Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪