Photo by Yosuke Ota on Unsplash

On this page

This chapter follows Classes in JavaScript.

JavaScript objects are mutable by default. Three built-ins shrink the set of allowed changes at the object level (shallowly).

preventExtensions, seal, freeze

API Add keys Delete keys Change values Change descriptors
Object.preventExtensions(obj) No Yes Yes Yes
Object.seal(obj) No No Yes No (except value write on data props)
Object.freeze(obj) No No No No
const config = { host: 'localhost', port: 3000 };
Object.freeze(config);
config.port = 8080; // silent fail in sloppy mode; TypeError in strict mode

Nested objects are not frozen unless you recurse. For deep trees, walk and freeze, or treat data as immutable with new copies (spread, structuredClone, Immer, etc.).

Checking state

Object.isExtensible(obj);
Object.isSealed(obj);
Object.isFrozen(obj);

Use freeze for constants exported from a module; use seal when the key set is fixed but values may still update.

Next: Proxy and Reflect.

Search articles

Type a word to search published articles.

Open the search page