Why Object.isFrozen ?
Object.isFrozen () is used to control whether an object is frozen. (An object is frozen by Object.freeze ()) Returns a boolean value that is true or incorrect. If the “True” value is returned, the existing properties and values in the object cannot be changed or new features added. If “False” returns, the property can be added or removed.
Syntax:
Object.isFrozen( obj )
obj: The object to be checked.
Return Value:
true if all of the following are true:
- The object is non-extensible, which indicates that new properties cannot be added to the object.
- The configurable attribute is false for all existing properties.
- The writable attribute is false for all existing data properties.
- If the object has no existing properties, the function returns true if the object is non-extensible.
Remarks
When the configurable property of an object property is incorrect, the properties cannot be changed and the feature cannot be removed. When the property of an object can be configured, the property property value cannot be changed.
JavaScript Object.isFrozen Examples
Example 1
let animals = {
dog: "dog",
cat: "cat",
bird: "bird"
}
let arr1 = Object.isFrozen(animals)
console.log(arr1)
Object.freeze(animals);
let arr2 = Object.isFrozen(animals);
console.log(arr2)
output:
false
true
Example 2
let person = {
hi: "hi guys..."
};
console.log(Object.isFrozen(person))
Object.freeze(person)
console.log(Object.isFrozen(person))
output:
false
true
Example 3
let people = {
person1: "alex",
person2: "maria",
person3: "rick"
}
people.person4 = "oliver";
let arr1 = Object.isFrozen(people);
console.log("object.isFrozen: " + arr1);
console.log(people);
Object.freeze(people);
people.person5 = "barry";
let arr2 = Object.isFrozen(people);
console.log("object.isFrozen: " + arr2);
console.log(people);
output:
object.isFrozen: false
{person1: “alex”, person2: “maria”, person3: “rick”, person4: “oliver”}
object.isFrozen: true
{person1: “alex”, person2: “maria”, person3: “rick”, person4: “oliver”}
Sources
Browser Support
| Chrome | 6 |
| Edge | yes |
| Firefox | 4 |
| Internet Explorer | 9 |
| Opera | 12 |
| Safari | 5.1 |
| Android webview | yes |
| Chrome for Android | yes |
| Edge mobile | yes |
| Firefox for Android | 4 |
| Opera Android | yes |