const const_name; const x;
The const keyword ensures that the variable created is read-only. But It doesn’t mean that the actual value to which the const variable reference is immutable. Even though the person variable is constant. However, you can change the value of its property. But you cannot reassign a different value to the person constant.
Object.freeze() method: If you want the value of the person object to be immutable, you have to freeze it by using the Object.freeze() method.
The Object.freeze() method is shallow, meaning that it can freeze the properties of the object, not the objects referenced by the properties.
But the person.address object is not immutable, you can add a new property to the person.address object as follows:
// No TypeError person.address.country = "India";
- const prevents reassignment.
- Object.freeze() prevents mutability.
|1.||Object freeze() method helps in preventing existing properties from being changed||const is a keyword that was introduced in ES6 (2015).|
Its syntax is -:
|It is helpful if we want some variable not to be declared twice.|
|3.||It takes one parameter as an Object.|
For example -:
const a = 10;
|4.||Its return value is an object.||If we define a variable with const then it cannot be reassigned.|
|5.||It also helps in preventing the new properties to be added to the specific object.||If we define a variable with const then its scope is blocked.|
Please Login to comment...