MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Object.freeze, Object.seal & Immutability0%
Study Materials
JavaScript for AdvancedProgress 0%
1.1Higher-Order Functions & Currying1.2Function Composition & Pipelining1.3Memoization & Pure Functions1.4Execution Context & Call Stack Deep Dive1.5Scope Chain & Lexical Environment
10.1TypedArrays & ArrayBuffers10.2WeakMap & WeakSet for Memory Management10.3Object.freeze, Object.seal & Immutability10.4Symbols & Well-Known Symbols
HomeTutorialsJavaScript for AdvancedObject.freeze, Object.seal & Immutability
Symbols & Well-Known Symbols

Object.freeze, Object.seal & Immutability

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study Materials

In state management systems (Redux, Zustand) and security configurations, accidental object mutation causes hard-to-track bugs. Modern JavaScript provides varying levels of object locking: Object.preventExtensions(), Object.seal(), and Object.freeze().


1. The Immutability Spectrum

1 ┌────────────────────────────────────────────────────────┐
2 │ Level 1: Object.preventExtensions(obj) │
3 │ • CANNOT add new properties │
4 │ • CAN modify existing properties │
5 │ • CAN delete existing properties │
6 ├────────────────────────────────────────────────────────┤
7 │ Level 2: Object.seal(obj) │
8 │ • CANNOT add new properties │
9 │ • CANNOT delete existing properties │
10 │ • CAN modify existing property values │
11 ├────────────────────────────────────────────────────────┤
12 │ Level 3: Object.freeze(obj) │
13 │ • CANNOT add new properties │
14 │ • CANNOT delete existing properties │
15 │ • CANNOT modify existing property values (Read-Only) │
16 └────────────────────────────────────────────────────────┘

2. Property Descriptors Explained

Under the hood, these methods configure internal Property Descriptors:

1const user = { name: 'Sarah' };
2 
3// Inspect property descriptor
4console.log(Object.getOwnPropertyDescriptor(user, 'name'));
5// { value: 'Sarah', writable: true, enumerable: true, configurable: true }
  • writable: If false, value cannot be reassigned.
  • configurable: If false, property cannot be deleted and descriptors cannot be modified.
  • enumerable: If false, hidden from for...in and Object.keys().
1// Object.freeze() sets:
2// 1. Object extensible = false
3// 2. All own properties: writable = false, configurable = false

3. Strict Mode Enforcement

In non-strict mode, attempting to mutate a frozen object fails silently. In strict mode ('use strict'), it throws a TypeError:

1'use strict';
2 
3const appConfig = Object.freeze({
4 apiEndpoint: 'https://api.example.com',
5 timeout: 5000
6});
7 
8// appConfig.timeout = 10000;
9// TypeError: Cannot assign to read only property 'timeout' of object '#<Object>'

4. The Critical Gotcha: Object.freeze is SHALLOW!

Object.freeze() only freezes top-level properties. Nested objects remain mutable references!

1const enterpriseSettings = Object.freeze({
2 appName: 'MasterPortal',
3 database: {
4 host: 'localhost', // NESTED OBJECT!
5 port: 5432
6 }
7});
8 
9// DANGER: Nested mutation succeeds!
10enterpriseSettings.database.port = 3306;
11console.log(enterpriseSettings.database.port); // 3306!

The Solution: Recursive Deep Freeze Utility

1function deepFreeze(obj) {
2 // Retrieve all own property names
3 const propNames = Object.getOwnPropertyNames(obj);
4 
5 // Freeze properties before freezing self
6 for (const name of propNames) {
7 const value = obj[name];
8 if (value && typeof value === 'object') {
9 deepFreeze(value); // Recursive call
10 }
11 }
12 
13 return Object.freeze(obj);
14}
15 
16const safeSettings = deepFreeze({
17 database: { port: 5432 }
18});
19 
20// safeSettings.database.port = 3306; // TypeError: Cannot assign to read only property!

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the primary difference between Object.seal() and Object.freeze()?
Question 2
What happens when code attempts to mutate a frozen object in strict mode ('use strict')?
Question 3
Does calling Object.freeze(obj) prevent modifications to nested objects (e.g. obj.nested.key = val)?
Question 4
Which method checks whether an object has been sealed?
Question 5
What property descriptor controls whether a property can be deleted with the delete operator?
Next Lesson

Symbols & Well-Known Symbols

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
WeakMap & WeakSet for Memory ManagementSymbols & Well-Known Symbols

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext