Freeze an object | Javascript

 const obj = { 
  name: "JSsnippets",
  age:29,
  address:{
      street : 'JS'
    }
};														

const frozenObject = Object.freeze(obj);

frozenObject.name = 'weLoveJS'; // Uncaught TypeError

//Although, we still can change a property’s value if it’s an object:

frozenObject.address.street = 'React'; // no error, new value is set


delete frozenObject.name // Cannot delete property 'name' of #<Object>


//We can check if an object is frozen by using
Object.isFrozen(obj) //true
 

AI Explanation

Explanation of the code: 1. An object `obj` is created with properties `name`, `age`, and `address`. 2. `Object.freeze(obj)` is used to freeze the object `obj`, making it immutable. 3. Trying to change the value of the property `name` directly results in a `TypeError` because the object is frozen and its properties cannot be modified. 4. However, since the property `address` is an object, its properties can still be modified even though the object is frozen. So, changing the `street` property of `address` is allowed. 5. Attempting to delete the property `name` from the frozen object results in an error because frozen objects do not allow for property deletion. 6. `Object.isFrozen(obj)` is used to check if the object `obj` is frozen, which returns `true`. In summary, `Object.freeze` is used to make an object immutable, preventing its properties from being added, modified, or deleted. However, properties that are objects themselves can still be modified.