Object.seal() vs Object.freeze() in JavaScript

JavaScript

In this article you will learn Object.seal() vs Object.freeze() in JavaScript. In previous article we have explained data types in JavaScript. Both method prevents to add & delete property from an object. The main difference between Object.seal and Object.freeze is that Object.seal can update the exist property but Object.freeze can\’t update the exist property.

Object.seal()

Object.seal()┬áprevents to add new property and also prevents to delete exist property from an object. If you have seal object then you can\’t add new property in this object and also you can’t delete exist property from this object but you can update exist property in this object.
For example –

var obj = {
    fname: 'web',
    lname: 'study'
};

Object.seal(obj); // seal the object

obj.age = 20; // add new property

console.log(obj); // { fname: 'web', lname: 'study'}

delete obj.fname;  // delete fname property

console.log(obj); // { fname: 'web', lname: 'study'}

obj.fname = 'javascript' // update exist property fname

console.log(obj); // { fname: 'javascript', lname: 'study'}

console.log(Object.isSealed(obj)) // true

In this example you have seen, we have created an object then seal this object using Object.seal() method.
If we want to add new property age in this object then it prevents to add age property and also it prevents to delete the exist property fname but we can update the exist property fname.

To check the object is sealed or not then you can use Object.isSealed() method. In the above example when we console the Object.isSealed(obj) then it prints the true.

Object.freeze()

Object.freeze() prevents to add, update and delete property from an object. You can\’t add, update and delete exist property from an object. Object.freeze makes the object immutable i.e, you can\’t change anything.
For example –

var obj = {
    fname: 'web',
    lname: 'study'
};

Object.freeze(obj);

obj.age = 20; // add new property

console.log(obj); // { fname: 'web', lname: 'study'}

delete obj.fname;  // delete fname property

console.log(obj); // { fname: 'web', lname: 'study'}

obj.fname = 'javascript' // update exist property fname

console.log(obj); // { fname: 'web', lname: 'study'}

console.log(Object.isFrozen(obj)) // true

In the above example you have seen, we have created an object then freeze this object using Object.freeze() method.
If we want to add new property age in this object then it prevents to add age property in this object and also it prevents to update/delete exist property fname.

To check the object is freezed or not then you can use Object.isFrozen() method. In the above example when we console the Object.isFrozen(obj) then it prints the true.

Conclusion

So in this article we have explained Object.seal() vs Object.freeze() in JavaScript. Using this tutorial anyone can easily understand Object.seal() vs Object.freeze() in JavaScript.

CreateUpdateDeleteRead
Object.sealNoYesNoYes
Object.freezeNoNoNoYes

If you want to improve or you find anything wrong/incorrect in this article then please leave a message using below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *

5 + 13 =