Curriculum
Concept 53 // Module 06: Objects & Prototypal Inheritance
Object Immutability: freeze(), seal() & preventExtensions()
Module 06: Objects & Prototypal Inheritance•Concept 53 of 100• 5 min read
Object Immutability: freeze(), seal() & preventExtensions()
JavaScript provides three levels of object immutability: `Object.preventExtensions()` (no new properties), `Object.seal()` (no adding or deleting properties), and `Object.freeze()` (completely read-only, non-configurable properties).
The Core Concept
Immutability guarantees prevent accidental state mutations in large applications, especially in predictable state stores like Redux and Zustand.
Syntax Reference
Object.preventExtensions(obj); // Can't add new props
Object.seal(obj); // Can't add/remove, can still edit existing
Object.freeze(obj); // Completely frozen (read-only)Live Interactive Studio
`Object.freeze()` locks all properties: `writable: false` and `configurable: false`.
JavaScript Editor
Loading...
Console Output
Waiting for output...
Key Code Patterns in Action
01
Comparing the 3 Immutability Levels
const user = { name: "Subhajit", role: "admin" };
Object.freeze(user);
// Modifications fail silently (or throw TypeError in strict mode)
user.name = "Alex";
delete user.role;
user.age = 25;
console.log(user); // { name: "Subhajit", role: "admin" }
console.log(Object.isFrozen(user)); // true`Object.freeze()` locks all properties: `writable: false` and `configurable: false`.
02
Deep Freeze Utility
function deepFreeze(obj) {
Object.keys(obj).forEach(prop => {
if (typeof obj[prop] === 'object' && obj[prop] !== null) {
deepFreeze(obj[prop]);
}
});
return Object.freeze(obj);
}
const config = deepFreeze({ api: { host: "localhost" } });
// config.api.host = "remote"; // ❌ Prevented by deep freeze!`Object.freeze()` is **shallow**. Nested objects inside a frozen parent can still be mutated unless recursively frozen.
03
1. Core Usage: Object Immutability
// Core syntax and usage for Object Immutability: freeze(), seal() & preventExtensions()
const feature = 'object-immutability-freeze-seal-preventextensions';
console.log('Concept 53:', feature);
console.log('Active:', true);Illustrates the fundamental invocation and syntax structure for Object Immutability: freeze(), seal() & preventExtensions().
04
2. Defensive Handling & Edge Case
// Edge-case defensive handling
function handleSafe(input) {
if (input === null || input === undefined) return 'safe-fallback';
return String(input).trim();
}
console.log(handleSafe('object-immutability-freeze-seal-preventextensions'));
console.log(handleSafe(null));Shows how to write defensive, safe JavaScript code that handles nullish or unexpected inputs when working with Object Immutability: freeze(), seal() & preventExtensions().
05
3. Clean Architecture & Production Pattern
// Production-grade pattern for Object Immutability: freeze(), seal() & preventExtensions()
const settings = Object.freeze({
id: 'object-immutability-freeze-seal-preventextensions',
ready: true,
timestamp: Date.now()
});
console.log('Settings initialized:', settings.id, settings.ready);Demonstrates clean, immutable production code avoiding unintended state mutations.
Real-World Engineering Takeaways
Constants & Configuration: Freezing application configs to prevent runtime tampering.
Defensive APIs: Returning frozen snapshots of internal engine states to prevent consumers from mutating them.
Security Hardening: Freezing prototypes (`Object.freeze(Object.prototype)`) to block prototype pollution attacks.
Under the Hood: Engine & Memory Mechanics
How JavaScript Executes This
When an object is frozen, V8 transitions the object's Map (Hidden Class) to a frozen shape where all property descriptors are marked non-writable and non-configurable.
Quick Knowledge Check
Test your understanding on these 10 questions (one by one):
Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model
What is the core purpose of Object Immutability: freeze(), seal() & preventExtensions() in JavaScript?
// Concept 53: Object Immutability: freeze(), seal() & preventExtensions()
// Understanding the core mental model1 / 10