Object.isFrozen()
ES5+Determines if an object is frozen.
Syntax
Object.isFrozen(obj)Parameters
obj Object The object to check
Return Value
true if the object is frozen, otherwise false
Examples
const obj = { a: 1 };
console.log(Object.isFrozen(obj));
Object.freeze(obj);
console.log(Object.isFrozen(obj)); 📌 When to Use
Use Object.isFrozen() to verify immutability before performing operations that assume an object cannot change, in defensive programming to validate inputs, or when debugging to check if objects are properly frozen in your application.
⚠️ Common Mistakes
Assuming Object.isFrozen() checks nested objects - it only checks the top level. Each nested object must be checked separately.
Confusing frozen with sealed - a frozen object is always sealed, but a sealed object is not necessarily frozen.
✅ Best Practices
Create an isDeepFrozen utility to check all nested objects: function isDeepFrozen(obj) { return Object.isFrozen(obj) && Object.values(obj).filter(v => typeof v === "object").every(isDeepFrozen); }
Use in assertions during development: console.assert(Object.isFrozen(config), "Config should be frozen")
⚡ Performance Notes
Object.isFrozen() is a very fast O(1) operation as it only checks an internal flag on the object. It does not iterate over properties. Safe to use in hot paths or conditional checks without performance concerns.
🌍 Real World Example
Defensive Configuration Validation
Validate that configuration objects are properly frozen before using them in critical application code.
function isDeepFrozen(obj) {
if (!Object.isFrozen(obj)) return false;
return Object.values(obj)
.filter(value => value !== null && typeof value === 'object')
.every(isDeepFrozen);
}
function initializeApp(config) {
// Defensive check - ensure config is immutable
if (!isDeepFrozen(config)) {
console.warn('Warning: Config should be frozen for safety');
// Optionally freeze it ourselves
// config = deepFreeze(config);
}
console.log('App initialized with:', config.appName);
}
// Test cases
const mutableConfig = { appName: 'MyApp', settings: { debug: true } };
console.log(Object.isFrozen(mutableConfig)); // false
Object.freeze(mutableConfig);
console.log(Object.isFrozen(mutableConfig)); // true
console.log(isDeepFrozen(mutableConfig)); // false (nested object not frozen)
Object.freeze(mutableConfig.settings);
console.log(isDeepFrozen(mutableConfig)); // true