Object.freeze and seal
Prevent changes to objects.
Object.freeze and seal is a free JavaScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Freeze Objects?
Immutability means data cannot change after creation. It prevents accidental mutations, makes code easier to reason about, and avoids whole classes of bugs.
JavaScript gives you Object.freeze and Object.seal to lock objects down.
Object.freeze Basics
Object.freeze makes an object fully read-only: you cannot add, remove, or change properties. It returns the same object, now frozen.
const user = Object.freeze({ name: 'Ada', age: 36 });
user.age = 99;
console.log(user.age);Checking With isFrozen
Object.isFrozen tells you whether an object is frozen. Useful before attempting an update.
const config = Object.freeze({ debug: false });
const settings = { theme: 'dark' };
console.log(Object.isFrozen(config));
console.log(Object.isFrozen(settings));Silent vs Strict Failure
In non-strict code, writes to a frozen object fail silently. In strict mode they throw a TypeError. Either way the value never changes.
'use strict';
const frozen = Object.freeze({ x: 1 });
try {
frozen.x = 2;
} catch (e) {
console.log(e.name);
}
console.log(frozen.x);Freeze Blocks New Properties
Freezing also prevents adding properties. The new key simply does not appear.
const point = Object.freeze({ x: 0 });
point.y = 5;
console.log(point.y);
console.log(Object.keys(point));Freeze Blocks Deletion
You also cannot delete properties from a frozen object. The property stays put.
const obj = Object.freeze({ keep: 'me' });
delete obj.keep;
console.log(obj.keep);Object.seal Is Different
Object.seal is less strict than freeze. You cannot add or remove properties, but you can still change existing values.
const sealed = Object.seal({ count: 1 });
sealed.count = 2;
sealed.extra = 3;
console.log(sealed.count);
console.log(sealed.extra);Checking With isSealed
Object.isSealed reports whether an object is sealed. Note that every frozen object is also sealed, but not vice versa.
const sealed = Object.seal({ a: 1 });
const frozen = Object.freeze({ b: 2 });
console.log(Object.isSealed(sealed));
console.log(Object.isSealed(frozen));
console.log(Object.isFrozen(sealed));freeze vs seal Summary
freeze: no add, no remove, no change. seal: no add, no remove, but change allowed. Choose seal when the shape is fixed but values evolve.
const f = Object.freeze({ v: 1 });
const s = Object.seal({ v: 1 });
f.v = 100;
s.v = 100;
console.log('frozen v:', f.v);
console.log('sealed v:', s.v);preventExtensions
The loosest lock is Object.preventExtensions: you cannot add properties, but you can change and delete existing ones. It is the weakest of the three.
const obj = Object.preventExtensions({ a: 1 });
obj.a = 2;
obj.b = 3;
console.log(obj.a);
console.log(obj.b);
console.log(Object.isExtensible(obj));Why It Matters
Freezing configuration and constants prevents accidental edits across a codebase. But remember: Object.freeze is shallow, so nested objects stay mutable. You will fix that next.
const SETTINGS = Object.freeze({ maxRetries: 3 });
function tryUpdate() {
SETTINGS.maxRetries = 10;
}
tryUpdate();
console.log(SETTINGS.maxRetries);Quick Check
What is the difference between Object.freeze and Object.seal?
Recap: freeze and seal
You learned to lock objects:
Object.freeze: no add, remove, or change. Check withisFrozen.Object.seal: no add or remove, but change allowed.preventExtensions: only blocks adding.- All three are shallow, which leads us to deep freezing next.
Frequently asked questions
Is the “Object.freeze and seal” lesson free?
Yes — the full text of “Object.freeze and seal” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Object.freeze and seal”?
Prevent changes to objects. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Object.freeze and seal” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Object.freeze and seal
- Shallow vs Deep Freezing
- Immutable Update Patterns
- structuredClone for Deep Copies