Skip to content
jshacks.dev
Fundamentals

Property Descriptors in JavaScript and Their Three Flags

Every property has three flags, writable, enumerable and configurable, that decide whether it can change, show in loops or be redefined. See the defaults.

8 min read
Summarize in AI
Flat illustration of an editor window full of code, with the JavaScript, Vite, npm and TypeScript logos.

Every property in JavaScript carries three invisible switches, and they explain why some properties never show up in Object.keys(), why an assignment can silently fail, and why class methods do not appear when you loop over an instance. Most code never touches them directly, and library code uses them constantly: to hide internals, to define constants and to make an API read-only. This post lists the flags, shows the default that surprises people with Object.defineProperty(), and walks through what each flag changes, using Node.js 24 as an ES module. It builds on the post on objects, and it leads into getters and setters, which are the other kind of property.

Reading a descriptor

Object.getOwnPropertyDescriptor() returns the attributes of one own property.

JavaScript
const user = { name: "Ada" };

console.log(Object.getOwnPropertyDescriptor(user, "name"));
console.log(Object.getOwnPropertyDescriptor([], "length"));
console.log(Object.getOwnPropertyDescriptor(Math, "PI"));

class Account {
  deposit() {}
}

console.log(Object.getOwnPropertyDescriptor(Account.prototype, "deposit").enumerable);
Text
{ value: 'Ada', writable: true, enumerable: true, configurable: true }
{ value: 0, writable: true, enumerable: false, configurable: false }
{
  value: 3.141592653589793,
  writable: false,
  enumerable: false,
  configurable: false
}
false

A property made by plain assignment has all three flags set to true. An array's length is writable but not enumerable or configurable, so it cannot be deleted. Math.PI is read-only in every way, which is why Math.PI = 3 does nothing. And class methods are non-enumerable, so a for...in loop over an instance does not list them.

The defineProperty default

Object.defineProperty() creates or changes a property from a descriptor. Any flag you leave out defaults to false for a new property, which is the opposite of assignment.

JavaScript
const order = {};

Object.defineProperty(order, "id", { value: 7 });

console.log(Object.getOwnPropertyDescriptor(order, "id"));
console.log(Object.keys(order));
console.log(order.id);
console.log(JSON.stringify(order));

order.id = 99;
console.log(order.id);
Text
{ value: 7, writable: false, enumerable: false, configurable: false }
[]
7
{}
file:///private/app/snippet.mjs:10
order.id = 99;
         ^

TypeError: Cannot assign to read only property 'id' of object '#<Object>'
    at file:///private/app/snippet.mjs:10:10
    at ModuleJob.run (node:internal/modules/esm/module_job:327:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:663:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:99:5)

Node.js v24.1.0

The property exists and has a value, and it is not writable, enumerable or configurable. It is invisible to Object.keys() and JSON.stringify(). The final assignment threw, because this file is an ES module and so strict. In a sloppy script the same write would be silently ignored, which is the version of the bug that is hard to spot. Always spell out all three flags when you define a property, so a reader does not have to remember the default.

writable

When writable is false, assigning a new value fails. In strict mode it throws a TypeError, and in sloppy mode the write is ignored.

JavaScript
const config = {};

Object.defineProperty(config, "version", {
  value: "1.0",
  writable: false,
  enumerable: true,
  configurable: true,
});

try {
  config.version = "2.0";
} catch (error) {
  console.log(error.name + ": " + error.message);
}

console.log(config.version);

Object.defineProperty(config, "version", { value: "2.0" });
console.log(config.version);
Text
TypeError: Cannot assign to read only property 'version' of object '#<Object>'
1.0
2.0

The direct assignment threw. Because configurable is still true, defineProperty can change the value anyway, so writable: false does not by itself make a property permanent. A read-only property that must stay fixed needs configurable: false as well.

enumerable

The enumerable flag decides whether a property appears in enumeration: Object.keys(), Object.values(), Object.entries(), for...in, object spread and JSON.stringify().

JavaScript
const record = { id: 1 };

Object.defineProperty(record, "internal", {
  value: "secret",
  writable: true,
  enumerable: false,
  configurable: true,
});

console.log(Object.keys(record));
console.log({ ...record });
console.log(JSON.stringify(record));
console.log(Object.getOwnPropertyNames(record));
console.log(Reflect.ownKeys(record));
console.log(record.internal);
console.log("internal" in record);
Text
[ 'id' ]
{ id: 1 }
{"id":1}
[ 'id', 'internal' ]
[ 'id', 'internal' ]
secret
true

The hidden property is skipped by all the enumeration tools and copy operations, but it is still there: reading it by name works, in finds it, and Object.getOwnPropertyNames() and Reflect.ownKeys() list it. A non-enumerable property is hidden from casual iteration and not protected from anyone who asks for it by name, so it is not a security feature.

This is how built-ins hide their methods. Everything on Array.prototype is non-enumerable, which is why for...in over an array does not list map and filter.

configurable

When configurable is false, the property cannot be deleted, and its descriptor cannot be changed, with one exception: a writable property may still become non-writable.

JavaScript
const limits = {};

Object.defineProperty(limits, "max", {
  value: 10,
  writable: true,
  enumerable: true,
  configurable: false,
});

try {
  delete limits.max;
} catch (error) {
  console.log(error.name + ": " + error.message);
}

try {
  Object.defineProperty(limits, "max", { enumerable: false });
} catch (error) {
  console.log(error.name + ": " + error.message);
}

limits.max = 20;
Object.defineProperty(limits, "max", { writable: false });
console.log(limits.max);

try {
  Object.defineProperty(limits, "max", { writable: true });
} catch (error) {
  console.log(error.name + ": " + error.message);
}
Text
TypeError: Cannot delete property 'max' of #<Object>
TypeError: Cannot redefine property: max
20
TypeError: Cannot redefine property: max

Deleting and redefining both threw. Setting the value while writable was still true worked, and lowering writable to false was allowed, but raising it back was rejected. That one-way rule is what makes configurable: false a real lock.

Defining several at once and copying faithfully

Object.defineProperties() takes a map of descriptors, and Object.getOwnPropertyDescriptors() returns all of an object's descriptors, which is the faithful way to clone an object including its accessors and flags.

JavaScript
const source = {
  id: 1,
  get label() {
    return "item-" + this.id;
  },
};

Object.defineProperty(source, "hidden", { value: 1, enumerable: false });

const spreadCopy = { ...source };
const faithfulCopy = Object.create(
  Object.getPrototypeOf(source),
  Object.getOwnPropertyDescriptors(source),
);

console.log(Object.getOwnPropertyDescriptor(spreadCopy, "label"));
console.log(typeof Object.getOwnPropertyDescriptor(faithfulCopy, "label").get);
console.log(Object.getOwnPropertyNames(spreadCopy));
console.log(Object.getOwnPropertyNames(faithfulCopy));
Text
{
  value: 'item-1',
  writable: true,
  enumerable: true,
  configurable: true
}
function
[ 'id', 'label' ]
[ 'id', 'label', 'hidden' ]

Spread evaluated the getter and stored its value as a plain data property, and it dropped the non-enumerable hidden. The descriptor-based copy kept the getter and the hidden property. Use spread for plain data and the descriptor copy when the object has accessors or flags you must preserve.

freeze and seal in terms of flags

Object.seal() sets configurable: false on every own property and stops new ones. Object.freeze() does the same and also sets writable: false on every data property.

JavaScript
const frozen = Object.freeze({ a: 1 });
const sealed = Object.seal({ a: 1 });

console.log(Object.getOwnPropertyDescriptor(frozen, "a"));
console.log(Object.getOwnPropertyDescriptor(sealed, "a"));
console.log(Object.isFrozen(frozen), Object.isSealed(frozen), Object.isFrozen(sealed));
Text
{ value: 1, writable: false, enumerable: true, configurable: false }
{ value: 1, writable: true, enumerable: true, configurable: false }
true true false

The output shows what the two shortcuts are made of. A frozen property is non-writable and non-configurable, and a sealed one is writable and non-configurable. Both are shallow: nested objects keep their own flags.

Accessor properties have their own descriptor

A property can be defined by functions instead of a stored value. Its descriptor then holds get and set and has no value or writable, and the enumerable and configurable flags still apply. The post on getters and setters covers them in detail, and a quick look at the descriptor shows the difference.

JavaScript
const temperature = {
  celsius: 20,
  get fahrenheit() {
    return this.celsius * 1.8 + 32;
  },
};

const data = Object.getOwnPropertyDescriptor(temperature, "celsius");
const accessor = Object.getOwnPropertyDescriptor(temperature, "fahrenheit");

console.log(Object.keys(data));
console.log(Object.keys(accessor));
console.log(typeof accessor.get, accessor.set);
Text
[ 'value', 'writable', 'enumerable', 'configurable' ]
[ 'get', 'set', 'enumerable', 'configurable' ]
function undefined

A data descriptor lists value and writable, and an accessor descriptor lists get and set, so a descriptor cannot have both. Mixing them in one call to defineProperty throws a TypeError. Tools that serialize or copy objects have to handle both shapes, which is why getOwnPropertyDescriptors is the accurate way to clone.

Debugging with descriptors

When a write silently fails or a property will not show up in a log, print its descriptor first. It answers the question in one line: is the value read-only, hidden or locked. Check the object and its prototype chain, since a setter or a read-only property on the prototype blocks assignment on the instance, which is a common surprise with frozen shared defaults. The prototype chain post shows how to walk that chain.

Where descriptors matter in practice

Three jobs justify reaching for them. Defining a constant on an object that other code might try to overwrite, as many libraries do for their version and feature flags. Hiding bookkeeping data from enumeration and serialization, such as an internal id on a model. And writing a polyfill, where the spec says a method is non-enumerable, and getting that wrong makes for...in over arrays suddenly list your function.

JavaScript
if (!Array.prototype.last) {
  Object.defineProperty(Array.prototype, "last", {
    value() {
      return this.at(-1);
    },
    writable: true,
    enumerable: false,
    configurable: true,
  });
}

for (const key in [10, 20]) {
  console.log(key);
}
console.log([10, 20].last());
Text
0
1
20

Only the indexes were listed, because the added method is non-enumerable. Had it been added by assignment, for...in would have printed last too. We would still avoid extending built-in prototypes in application code, since it collides with future standards, and the example shows the technique and not an endorsement.

Where this advice stops

For ordinary application data, descriptors are noise. Use plain assignment, and use a Proxy or a closure if you need to guard writes dynamically. In TypeScript, readonly properties and as const express immutability at compile time with no runtime cost.

Do not use non-enumerable properties to store secrets. They are one Object.getOwnPropertyNames() away from being read.

Descriptors are defined in the specification under property attributes, and MDN documents the method on its Object.defineProperty reference page.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.