What is the Object.freeze() method in JavaScript ?
Last Updated :
07 Feb, 2024
The Object.freeze()
method in JavaScript is used to freeze an object, making it immutable. Once an object is frozen, its properties cannot be added, modified, or removed. This ensures that the object’s state remains unchanged, preventing accidental alterations.
Syntax:
Object.freeze(obj)
Example: Here, the object “obj2” has been assigned property from object “obj1”, and the properties of “obj1” are frozen therefore new properties and values are prevented from being added to “obj2”.
Javascript
const obj1 = { property1: 'initial_data' };
const obj2 = Object.freeze(obj1);
obj2.property1 = 'new_data' ;
console.log(obj2.property1);
|
Share your thoughts in the comments
Please Login to comment...