Skip to main content
Practice Problems

Object.freeze(), object.seal() and object.assign() in JavaScript

Object Static Methods

JavaScript provides several important static methods on Object for controlling object mutability and copying properties.


Object.freeze()

Makes an object completely immutable β€” no adding, removing, or modifying properties:

javascript
const config = Object.freeze({ apiUrl: "https://api.example.com", timeout: 5000 }); config.apiUrl = "changed"; // ❌ Silently fails (throws in strict mode) config.newProp = "value"; // ❌ Silently fails delete config.timeout; // ❌ Silently fails console.log(config.apiUrl); // "https://api.example.com" (unchanged)

Shallow Freeze

Object.freeze() is shallow β€” nested objects are not frozen:

javascript
const user = Object.freeze({ name: "Alice", address: { city: "Kyiv" } // Nested object is NOT frozen }); user.name = "Bob"; // ❌ Fails user.address.city = "Lviv"; // βœ… Works! (nested object is mutable)

Deep Freeze

javascript
function deepFreeze(obj) { Object.freeze(obj); Object.values(obj).forEach(value => { if (typeof value === "object" && value !== null) { deepFreeze(value); } }); return obj; }

Object.seal()

Prevents adding or removing properties, but allows modifying existing values:

javascript
const user = Object.seal({ name: "Alice", age: 25 }); user.name = "Bob"; // βœ… Can modify existing properties user.email = "a@b.com"; // ❌ Cannot add new properties delete user.age; // ❌ Cannot delete properties console.log(user); // { name: "Bob", age: 25 }

Comparison: freeze vs seal vs regular

OperationRegular ObjectObject.seal()Object.freeze()
Read propertiesβœ…βœ…βœ…
Modify valuesβœ…βœ…βŒ
Add propertiesβœ…βŒβŒ
Delete propertiesβœ…βŒβŒ
Reconfigure descriptorsβœ…βŒβŒ

Checking Object State

javascript
Object.isFrozen(frozenObj); // true Object.isSealed(sealedObj); // true Object.isExtensible(regularObj); // true (can add properties)

Object.assign()

Copies enumerable own properties from one or more source objects to a target object:

javascript
const target = { a: 1 }; const source1 = { b: 2 }; const source2 = { c: 3 }; const result = Object.assign(target, source1, source2); console.log(result); // { a: 1, b: 2, c: 3 } console.log(target); // { a: 1, b: 2, c: 3 } β€” target IS modified! console.log(result === target); // true

Common Patterns

javascript
// Merge without mutating (create new object) const merged = Object.assign({}, defaults, userSettings); // Same with spread (preferred) const merged = { ...defaults, ...userSettings }; // Clone an object (shallow) const clone = Object.assign({}, original); const clone = { ...original }; // Same thing

Shallow Copy Warning

javascript
const original = { name: "Alice", address: { city: "Kyiv" } }; const copy = Object.assign({}, original); copy.address.city = "Lviv"; console.log(original.address.city); // "Lviv" β€” nested object shared!

Object.preventExtensions()

Prevents adding new properties, but allows modification and deletion:

javascript
const obj = Object.preventExtensions({ a: 1, b: 2 }); obj.a = 10; // βœ… Can modify delete obj.b; // βœ… Can delete obj.c = 3; // ❌ Cannot add

Important:

Use Object.freeze() for constants and configuration objects. Use Object.seal() when you want to allow modifications but prevent structural changes. Prefer the spread operator ({ ...obj }) over Object.assign() for creating copies. Remember all these operations are shallow.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.

Finished reading?
Practice Problems