Testing Object Mutability States
You'll learn how to check whether an object can be modified, sealed, or frozen.
In the previous lesson, you learned how to lock down objects using Object.preventExtensions(), Object.seal(), and Object.freeze(). But what if you receive an object and need to check its current state? JavaScript provides three companion methods to test mutability.
The Three Test Methods
Object.isExtensible(obj)
Returns true if new properties can be added to the object, false otherwise. A normal object is extensible; one that's been prevented from extensions, sealed, or frozen is not extensible.
Object.isSealed(obj)
Returns true if the object is sealed — meaning it's non-extensible and all properties are non-configurable (can't be deleted or reconfigured).
Object.isFrozen(obj)
Returns true if the object is frozen — the strictest state where it's sealed and all properties are non-writable (values can't change).
Example in Action
const user = { name: "Alice" };
console.log(Object.isExtensible(user)); // true (normal state)
console.log(Object.isSealed(user)); // false
console.log(Object.isFrozen(user)); // false
Object.seal(user);
console.log(Object.isExtensible(user)); // false (can't add properties)
console.log(Object.isSealed(user)); // true
console.log(Object.isFrozen(user)); // false (values still writable)
Object.freeze(user);
console.log(Object.isFrozen(user)); // true (completely locked)
Why This Matters
When working with objects from libraries, APIs, or other modules, these methods let you verify constraints before attempting modifications. They're defensive programming tools that prevent runtime errors.
Key Takeaway: Use isExtensible(), isSealed(), and isFrozen() to test an object's mutability state before modifying it, helping you write safer, more predictable code.