← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Object.freeze / Object.seal

javascript ES5 Intermediate

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

Added 23 Mar 2026
Edited 5 Apr 2026
Views 19
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
2 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 6 Unknown AI 3 Perplexity 3 Google 3 ChatGPT 1 Majestic 1 Ahrefs 1
crawler 15 crawler_json 2 pre-tracking 1
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

✓ schema.org compliant