Curriculum
Object.freeze() and Object.seal() in JavaScript are important built-in object methods used to control object modifications and improve data security. Understanding Object.freeze() and Object.seal() is essential for beginners because these methods help developers protect object data, prevent unwanted changes, and build reliable JavaScript applications.
Objects in JavaScript are:
Meaning:
Example:
let user = {
name: "Rahul"
};
user.age = 25;
Sometimes developers want to:
JavaScript provides:
Object.freeze()Object.seal()These methods are widely used in:
Understanding Object.freeze() and Object.seal() helps developers manage object security efficiently.
These methods help developers:
Modern JavaScript applications frequently use controlled object structures.
Object.freeze():
After freezing:
Syntax:
Object.freeze(objectName)
Example:
let user = {
name: "Rahul"
};
Object.freeze(user);
user.name = "Aman";
console.log(user.name);
Output:
Rahul
The update fails because:
Example:
let student = {
name: "Rahul"
};
Object.freeze(student);
student.age = 20;
console.log(student);
Output:
{name: "Rahul"}
New properties cannot be added.
Example:
let car = {
brand: "Toyota"
};
Object.freeze(car);
delete car.brand;
console.log(car);
Output:
{brand: "Toyota"}
Frozen objects cannot lose properties.
Object.seal():
After sealing:
Syntax:
Object.seal(objectName)
Example:
let user = {
name: "Rahul"
};
Object.seal(user);
user.name = "Aman";
console.log(user.name);
Output:
Aman
Updates are allowed in sealed objects.
Example:
let product = {
name: "Laptop"
};
Object.seal(product);
product.price = 50000;
console.log(product);
Output:
{name: "Laptop"}
New properties cannot be added.
Example:
let employee = {
name: "Rahul"
};
Object.seal(employee);
delete employee.name;
console.log(employee);
Output:
{name: "Rahul"}
Sealed objects keep existing properties.
| Feature | Object.freeze() | Object.seal() |
|---|---|---|
| Add properties | Not allowed | Not allowed |
| Delete properties | Not allowed | Not allowed |
| Update properties | Not allowed | Allowed |
This is the most important difference.
JavaScript provides:
Object.isFrozen()Example:
let user = {
name: "Rahul"
};
Object.freeze(user);
console.log(Object.isFrozen(user));
Output:
true
This checks whether an object is frozen.
JavaScript provides:
Object.isSealed()Example:
let user = {
name: "Rahul"
};
Object.seal(user);
console.log(Object.isSealed(user));
Output:
true
This checks whether an object is sealed.
Example:
let student = {
name: "Rahul",
address: {
city: "Jaipur"
}
};
Object.freeze(student);
student.address.city = "Delhi";
console.log(student.address.city);
Output:
Delhi
Important:
Object.freeze() creates shallow freezingNested objects remain mutable unless frozen separately.
These methods are used in:
Modern applications frequently protect critical object data.
Example:
let config = {
appName: "MyApp",
version: "1.0"
};
Object.freeze(config);
Configuration data should remain unchanged.
Example:
let permissions = {
admin: true
};
Object.seal(permissions);
permissions.admin = false;
console.log(permissions.admin);
Output:
false
Permission values can still change in sealed objects.
Beginners often:
Incorrect expectation:
Object.freeze(user);
This does not deeply freeze nested objects automatically.
Benefits include:
These methods improve reliable application development.
Best practices include:
Object.freeze() for constant dataObject.seal() for controlled updatesReadable and secure object management improves maintainability.
Understanding Object.freeze() and Object.seal() helps developers:
These methods are important in modern web development.
Object.freeze() and Object.seal() in JavaScript are built-in methods used to control object modifications. Object.freeze() completely locks objects, while Object.seal() allows updates but prevents adding or deleting properties. These methods are widely used in APIs, dashboards, state management, and secure JavaScript applications.
Object.freeze() prevents adding, updating, and deleting object properties.
Object.seal() prevents adding and deleting properties but allows updates.
No, frozen objects cannot be modified.
No, it creates shallow freezing only.
They are used in APIs, dashboards, configuration systems, ReactJS, and secure web applications.
WhatsApp us