Object Control
- This lesson covers techniques to control object properties and behavior.
Introduction to Object Control
In JavaScript, objects are mutable by default, which means:
Properties can be added
Properties can be removed
Property values can be modified
While flexibility is powerful, it can also lead to:
Accidental changes
Security issues
Bugs in large applications
To control how objects behave, JavaScript provides object control methods.
In this lesson, we will study:
Object.freeze()
Object.seal()
Why Object Control is Important
In real-world applications:
Some objects should remain constant
Configuration objects should not be modified
API response data should be protected
Shared objects should be safe from mutation
Object.freeze()
What is Object.freeze() ?
Object.freeze() is a method that completely locks an object.
Once an object is frozen:
You cannot add new properties
You cannot delete existing properties
You cannot modify existing property values
The object becomes fully immutable.
Syntax of Object.freeze()
Object.freeze(objectName);
Object.freeze Method
Locks an object to prevent any modifications, additions, or deletions.
// Using Object.freeze()
const user = {
name: "Amit",
age: 25
};
Object.freeze(user);
user.age = 30; // Not allowed
user.city = "Delhi"; // Not allowed
delete user.name; // Not allowed
console.log(user); // { name: "Amit", age: 25 }
How Object.freeze() Works
After freezing:
Write operations fail silently (in non-strict mode)
Errors are thrown in strict mode
Object structure is completely locked
Object.freeze in Strict Mode
Throws an error when trying to modify a frozen object in strict mode.
// Object.freeze with strict mode
"use strict";
const product = {
price: 1000
};
Object.freeze(product);
product.price = 1200; // Error in strict mode
In strict mode, JavaScript throws an error instead of failing silently.
Checking if an Object is Frozen
Object.isFrozen(user); // true
Important Point: Shallow Freeze
Object.freeze() performs a shallow freeze.
This means:
Top-level properties are frozen
Nested objects can still be modified
Checking Frozen Object and Shallow Freeze
Verifies frozen state and shows that nested objects can still be modified.
// Check if object is frozen
const user = {
name: "Amit"
};
Object.freeze(user);
console.log(Object.isFrozen(user)); // true
// Shallow freeze example
const student = {
name: "Ravi",
marks: {
math: 80
}
};
Object.freeze(student);
// Nested object can still be modified
student.marks.math = 90;
console.log(student.marks.math); // 90
When to Use Object.freeze()
Configuration objects
Constants
API response protection
Prevent accidental mutation
Security-sensitive data
Object.seal()
What is Object.seal() ?
Object.seal() partially locks an object.
After sealing an object:
You cannot add new properties
You cannot delete existing properties
You can modify existing property values
The structure is fixed, but values are changeable.
Syntax of Object.seal()
Object.seal(objectName);
Object.seal Method
Locks object structure but allows updating existing property values.
// Using Object.seal()
const employee = {
name: "Neha",
salary: 50000
};
Object.seal(employee);
employee.salary = 60000; // Allowed
employee.department = "HR"; // Not allowed
delete employee.name; // Not allowed
console.log(employee); // { name: "Neha", salary: 60000 }
How Object.seal() Works
After sealing:
Object keys are fixed
Property values can still change
Object shape is preserved
Checking if an Object is Sealed
Object.isSealed(employee); // true
Object.seal Behavior and Check
Keeps object structure fixed while allowing value updates and checking seal status.
// Check if object is sealed
const employee = {
name: "Neha",
salary: 50000
};
Object.seal(employee);
console.log(Object.isSealed(employee)); // true
// Shallow seal example
const order = {
id: 101,
details: {
amount: 2000
}
};
Object.seal(order);
// Nested object can still be modified
order.details.amount = 2500;
console.log(order.details.amount); // 2500
- freeze vs seal
Feature Object.freeze() Object.seal() Add properties Not allowed Not allowed Delete properties Not allowed Not allowed Modify values Not allowed Allowed Object mutability Fully immutable Partially mutable Use case Read-only objects Controlled objects
Analogy
Object.freeze()
Like a sealed glass box: you can see it, but cannot touch or change anything.Object.seal()
Like a locked cupboard: doors cannot be changed, but items inside can be rearranged.
Common Mistakes
Assuming freeze blocks nested objects
Confusing seal with freeze
Forgetting shallow behavior
Expecting errors in non-strict mode
Best Practices
Use Object.freeze() for constants and configs
Use Object.seal() for controlled updates
Combine with strict mode for safety
Avoid deep mutation assumptions
Document frozen/sealed objects clearly