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

Object.freeze / Object.seal

JavaScript ES5 Intermediate
debt(d5/e3/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). TypeScript can warn about reassignment to frozen objects, but the canonical misconception—shallow vs. deep freezing—requires runtime testing or code review to catch. Most linters don't flag freeze misuse by default.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix explicitly recommends swapping to deepFreeze() or using TypeScript readonly—a one-component refactor. Replacing Object.freeze() calls with a safer pattern is straightforward and localized.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). Object.freeze() decisions affect only the objects being frozen and their call sites. It doesn't shape system architecture or create cross-cutting obligations like shared mutable state would. The burden is confined to the immutability strategy of individual data structures.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception directly contradicts developer intuition: freeze() *sounds* like it makes the whole object immutable, but it only freezes the top level. Nested objects remain mutable—a documented gotcha that contradicts the obvious expectation. Additionally, silent failure in non-strict mode adds a second serious trap.

About DEBT scoring →

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 42
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 1 ping S 0 pings S 1 ping M 1 ping T 1 ping W 1 ping T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 7 Google 5 Perplexity 4 Scrapy 4 Unknown AI 3 Ahrefs 3 Majestic 2 ChatGPT 1 Claude 1 Bing 1 Meta AI 1 Sogou 1
crawler 29 crawler_json 3 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