Both freeze and seal are used to create non extensible objects in JavaScript, but there are plenty of differences between them. Object.seal() allows changes to the existing properties of an object whereas Object.freeze() does not allow so. Object.freeze() makes an object immune to everything even little changes cannot be made. Object.seal() prevents from deletion of existing properties but cannot prevent them from external changes.
Syntax:
Object.freeze(objectname);
Example 1: Depicts implementation of Object.seal().
<script> // creates an object var obj = { // assigns 10 to value value: 10 }; // creates a non-extensible object Object.seal(obj); // the value gets updated to 20 obj.value = 20; console.log(obj.value); </script> |
Output:
20
Example-1 depicts how Object.seal() is used to create a non-extensible object, but that does not prevent the value of the object to be changed and it is seen that the value gets updated to 20.
Syntax:
Object.seal(objectname);
Example 2: Depicts implementation of Object.freeze().
<script> // creates an object var obj = { // assigns 10 to value value: 10 }; // creates a non-extensible object Object.freeze(obj); // updates the value obj.value = 20; // but cannot change the existing value console.log(obj.value); </script> |
Output:
10
Example-2 depicts how Object.freeze() is used to create a non-extensible object, but where the existing value of the object is prevented from being changed and 10 is given as the output.
Recommended Posts:
- Difference between Object.freeze() and const in JavaScript
- Object.seal( ) In JavaScript
- Object.freeze( ) in JavaScript
- Difference between var and let in JavaScript
- Difference between CSS and JavaScript
- Difference between JavaScript and Php
- Difference between Java and JavaScript
- Difference between TypeScript and JavaScript
- Difference between == and === operator in JavaScript
- What's the difference between JavaScript and JScript?
- What is the difference between every() and some() methods in JavaScript ?
- Difference between Python and JavaScript
- Difference between JavaScript and VBScript
- Difference between != and !== operator in JavaScript
- Difference between JavaScript and C Sharp
- Difference between stopPropagation vs stopImmediatePropagation in JavaScript
- Difference between Chosen and Select2 in JavaScript
- Difference between substr() and substring() in JavaScript
- Difference between DOM parentNode and parentElement in JavaScript
- What is the difference between call and apply in JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


