Object.freeze / Object.seal
TL;DR
Object.freeze() prevents all property changes (add/modify/delete), Object.seal() prevents add/delete but allows modification — both are shallow, not deep.
Explanation
Object.freeze(obj): no new properties, no changes, no deletions. Silently fails in non-strict mode, throws TypeError in strict mode. Object.isFrozen() checks. Object.seal(obj): no new/deleted properties, existing writable properties can change. Readonly check with Object.isSealed(). Both are SHALLOW — nested objects remain mutable. Deep freeze requires recursion. Use cases: constants, config objects, action type maps in Redux, enums before JS had them. For deep immutability: recursively freeze, use Immer, or use TypeScript readonly.
Common Misconception
✗ Object.freeze() deep-freezes nested objects — it's shallow. Nested objects remain mutable unless explicitly frozen.
Why It Matters
Freezing config and constant objects prevents accidental mutation that causes hard-to-find bugs, especially in shared state.
Common Mistakes
- Assuming freeze is deep — nested objects still mutable.
- Not knowing freeze silently fails in non-strict mode.
- Using freeze on large objects in hot paths — slight performance cost.
Code Examples
✗ Vulnerable
const CONFIG = { db: { host: 'localhost', port: 5432 } };
Object.freeze(CONFIG);
CONFIG.db.port = 9999; // Works! db is not frozen
✓ Fixed
function deepFreeze(obj) {
Object.getOwnPropertyNames(obj).forEach(name => {
const val = obj[name];
if (val && typeof val === 'object') deepFreeze(val);
});
return Object.freeze(obj);
}
const CONFIG = deepFreeze({ db: { host: 'localhost', port: 5432 } });
CONFIG.db.port = 9999; // TypeError in strict mode
References
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Edited
5 Apr 2026
Views
19
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 6
Unknown AI 3
Perplexity 3
Google 3
ChatGPT 1
Majestic 1
Ahrefs 1
Also referenced
How they use it
crawler 15
crawler_json 2
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Low
⚡ Quick Fix
Use deepFreeze() for truly immutable config objects. Prefer TypeScript readonly for compile-time protection. Use Object.freeze() for simple flat objects.
📦 Applies To
javascript ES5
web
cli
🔗 Prerequisites
🔍 Detection Hints
Object.freeze\(
Auto-detectable:
✗ No
typescript
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: Low
Context: File