JavaScript preventExtensions(), seal() and freeze()
Quick answer
Object integrity operations restrict changes to an object’s own structure. Freezing is stronger than sealing, but neither recursively freezes nested data.
How it works
preventExtensions disallows new own properties. seal additionally marks existing own properties nonconfigurable; writable data properties can still change value. freeze additionally marks own data properties nonwritable. Accessor setters may still act, and nested object references remain mutable unless separately restricted. Strict assignments that fail throw TypeError. Test status with the corresponding Object.is* predicates.
const o = Object.seal({n:1}); o.n = 4;console.log(o.n,Object.isSealed(o),Object.isFrozen(o));Expected output in Node.js:
4 true falseSealing allows existing writable data values to change.
const o = Object.freeze({nested:{n:1}}); o.nested.n=9;console.log(o.nested.n,Object.isFrozen(o),Object.isFrozen(o.nested));Expected output in Node.js:
9 true falseFreezing is shallow.
Common mistakes
Freezing an outer object is not a general guarantee of deep immutability.
Practice
Compare descriptors and status after each integrity operation on separate objects.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript preventExtensions(), seal() and freeze() — Knowledge Check
Apply the taught concept and predict the output.
Which operation prevents additions without itself making existing properties nonconfigurable?
Can a writable data value change on a sealed ordinary object?
Does Object.freeze recursively freeze nested objects?
What is the exact console output in JavaScript preventExtensions(), seal() and freeze() example 1?
const o = Object.seal({n:1}); o.n = 4;console.log(o.n,Object.isSealed(o),Object.isFrozen(o));What is the exact console output in JavaScript preventExtensions(), seal() and freeze() example 2?
const o = Object.freeze({nested:{n:1}}); o.nested.n=9;console.log(o.nested.n,Object.isFrozen(o),Object.isFrozen(o.nested));