Skip to content

Object.freeze()

Object.freeze() makes an object immutable — you cannot add, delete, or change its properties. It’s a shallow freeze.

const config = Object.freeze({
apiUrl: 'https://api.example.com',
timeout: 5000
});
config.apiUrl = 'https://evil.com'; // ❌ Fails silently (strict mode: throws)
config.newProp = 'test'; // ❌ Cannot add
delete config.timeout; // ❌ Cannot delete
Object.isFrozen(config); // true
const config = Object.freeze({
settings: { theme: 'dark' }
});
config.settings.theme = 'light'; // ✅ Allowed — nested object is not frozen
function deepFreeze(obj) {
Object.keys(obj).forEach(key => {
if (typeof obj[key] === 'object' && obj[key] !== null) {
deepFreeze(obj[key]);
}
});
return Object.freeze(obj);
}
  • Object.freeze() makes objects immutable (shallow)
  • Use for configuration objects and constants
  • Deep freeze required for nested objects
  • Check with Object.isFrozen()