JavaScript Academy · Lesson

Object.freeze and seal

Prevent changes to objects.

Lesson 1 of 413 steps

Object.freeze and seal is a free JavaScript Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.

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 with isFrozen.
  • Object.seal: no add or remove, but change allowed.
  • preventExtensions: only blocks adding.
  • All three are shallow, which leads us to deep freezing next.
Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “Object.freeze and seal” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “Object.freeze and seal”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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

  1. Object.freeze and seal
  2. Shallow vs Deep Freezing
  3. Immutable Update Patterns
  4. structuredClone for Deep Copies
← Back to JavaScript Academy