Object.isExtensible()

Determines whether new properties can still be added to an object.

Signature
Object.isExtensible(obj)
  • Added in JavaScript ES5 (2009)

Parameters

ParameterTypeDescription
$objObjectThe object to check for extensibility. Non-object arguments are treated as non-extensible.

Return value

booleantrue if the given object is extensible, otherwise false.

Changelog

  • PHP ES2015Passing a non-object returns false instead of throwing a TypeError, because primitives are non-extensible by definition.

Object.isExtensible() answers one narrow question: can this object still gain new properties? Objects are extensible by default and become non-extensible through Object.preventExtensions(), Object.seal(), Object.freeze() or Reflect.preventExtensions(). All three integrity levels imply non-extensible, so this is the cheapest test for "has anything been locked down at all".

When to reach for it

Use it as a precondition before code that attaches a property to an object it did not create — caching a computed value on a caller-supplied object, tagging entities with an internal id, or adding a listener slot. If the object is not extensible, branch to a WeakMap or a copy rather than letting the write vanish.

What trips people up

It says nothing about the properties already there. A non-extensible object can have every existing property freely writable and deletable, so isExtensible() === false does not mean immutable — use Object.isFrozen() for that.

Since ES2015, primitives return false instead of throwing, which means a check written as a guard silently takes the "locked" branch for a number or a string.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Check before attaching a cache or id property to a caller-supplied object
  • Fall back to a WeakMap for metadata when the target object is locked
  • Debug why an assignment created no property at all
  • Assert in a test that a factory locked its returned object

Frequently asked questions

Does Object.isExtensible() returning false mean the object is immutable?

No — it only means no new properties can be added; existing properties may still be writable and deletable, so use Object.isFrozen() to test for immutability.

What makes an object non-extensible?

Object.preventExtensions(), Object.seal(), Object.freeze() and Reflect.preventExtensions() all set the object non-extensible, and the change cannot be reversed.

What does Object.isExtensible() return for primitives?

false since ES2015 — primitives cannot gain properties, so they are reported as non-extensible instead of throwing a TypeError as they did in ES5.

More examples

Guarding metadata attachment

The open object gets the property directly, while the frozen one routes to the WeakMap instead of losing the write silently.

Example · javascript
const meta = new WeakMap();

function tag(obj, id) {
  if (Object.isExtensible(obj)) {
    obj.__id = id;
    return "attached inline";
  }
  meta.set(obj, id);
  return "stored in WeakMap";
}

const open = {};
const locked = Object.freeze({});
console.log(tag(open, 1), open.__id);
console.log(tag(locked, 2), meta.get(locked));

Non-extensible is not immutable

Extensibility is false yet the object is freely mutated — proof that this check alone never tells you an object is immutable.

Example · javascript
const o = Object.preventExtensions({ a: 1, b: 2 });

console.log(Object.isExtensible(o));   // false
o.a = 99;                              // still writable
delete o.b;                            // still deletable
console.log(o, Object.isFrozen(o));

Extensibility across the levels

Only the plain object is extensible; all three integrity levels set extensibility to false, so this check cannot distinguish between them.

Example · javascript
const states = {
  plain: {},
  prevented: Object.preventExtensions({ a: 1 }),
  sealed: Object.seal({ a: 1 }),
  frozen: Object.freeze({ a: 1 }),
};

for (const [name, o] of Object.entries(states)) {
  console.log(name, Object.isExtensible(o));
}

Discussion

  • Be the first to comment on this lesson.