JavaScript Property Attributes: Writable, Enumerable, Configurable and Object.defineProperty
Look under the hood of JavaScript properties: data vs accessor descriptors, the writable, enumerable and configurable attributes, Object.defineProperty and defineProperties, defaults, getOwnPropertyDescriptor(s), copying getters correctly, read-only and hidden properties, and interview rules.
kvi5hnuCode Sprout team member6 min readOn this page
This article opens Part 14: Metaprogramming: code that inspects and changes how objects themselves behave. The first layer is the property attributes. Every property you've created so far has hidden settings that decide whether it can be changed, whether it shows up in loops, and whether it can be deleted. Built-ins use them everywhere (that's why Math.PI = 3 does nothing and why for...in doesn't list toString), and libraries use them to create read-only, hidden or computed properties.
Property descriptors
Each property is described by a descriptor object. There are two kinds:

| Attribute | Data property | Accessor property | Meaning |
|---|---|---|---|
value | yes | the stored value | |
writable | yes | can assignment change value? | |
get / set | yes | functions called on read / write (article 53) | |
enumerable | yes | yes | is it listed by for...in, Object.keys, JSON.stringify, spread? |
configurable | yes | yes | can it be deleted, or its attributes changed? |
Object.getOwnPropertyDescriptor(obj, key) reads one:
const user = {
name: "Ana",
get initial() {
return this.name[0];
},
};
console.log(Object.getOwnPropertyDescriptor(user, "name"));
console.log(Object.getOwnPropertyDescriptor(user, "initial"));
console.log(Object.getOwnPropertyDescriptor(user, "toString")); // inherited, not own
{ value: 'Ana', writable: true, enumerable: true, configurable: true }
{
get: [Function: get initial],
set: undefined,
enumerable: true,
configurable: true
}
undefined
It only looks at own properties; for inherited ones, ask the prototype that owns them. Object.getOwnPropertyDescriptors(obj) returns all of them at once, keyed by property name.
Object.defineProperty
Object.defineProperty(obj, key, descriptor) creates a property, or changes an existing one, with exactly the attributes you specify. It returns the object.
The defaults are the opposite of normal assignment. Properties created by assignment or object literals get true for all three flags; with defineProperty, any flag you leave out is false:
const order = {};
Object.defineProperty(order, "id", { value: 7 });
console.log(Object.getOwnPropertyDescriptor(order, "id"));
order.id = 99; // silently ignored (not strict mode)
console.log(order.id, Object.keys(order), JSON.stringify(order), "id" in order);
{ value: 7, writable: false, enumerable: false, configurable: false }
7 [] {} true
The property exists ("id" in order is true) but is read-only, hidden from Object.keys and JSON, and permanent.
writable
A non-writable data property ignores assignments in sloppy mode and throws a TypeError in strict mode (which includes modules and classes):
"use strict";
const config = Object.defineProperty({}, "version", { value: "2.1", enumerable: true });
try {
config.version = "3.0";
} catch (err) {
console.log(err.name, "-", config.version);
}
TypeError - 2.1
A subtle consequence: a read-only property on a prototype also blocks assignment on objects that inherit it. The assignment doesn't create an own property:
const base = Object.defineProperty({}, "kind", { value: "base" });
const child = Object.create(base);
child.kind = "child"; // ignored: inherited kind is not writable
console.log(child.kind, Object.hasOwn(child, "kind"));
Object.defineProperty(child, "kind", { value: "child", writable: true, enumerable: true, configurable: true });
console.log(child.kind, Object.hasOwn(child, "kind"));
base false
child true
defineProperty still works, because it defines an own property directly instead of assigning.
enumerable
Non-enumerable properties still exist and can be read normally; they're just skipped by the APIs that list "normal" properties:
| Includes non-enumerable keys? | API |
|---|---|
| No | for...in, Object.keys/values/entries, JSON.stringify, {...spread}, Object.assign |
| Yes | Object.getOwnPropertyNames (strings), Reflect.ownKeys (strings and symbols), in, Object.hasOwn |
const point = Object.defineProperties({}, {
x: { value: 1, enumerable: true },
y: { value: 2, enumerable: true },
cacheKey: { value: "p:1:2" }, // not enumerable
});
console.log(point, Object.keys(point));
console.log(Object.getOwnPropertyNames(point), point.cacheKey);
console.log(point.propertyIsEnumerable("cacheKey"));
{ x: 1, y: 2 } [ 'x', 'y' ]
[ 'x', 'y', 'cacheKey' ] p:1:2
false
This is why built-in methods don't show up when you loop over objects: methods on Object.prototype, Array.prototype and class prototypes are non-enumerable. Methods defined with class syntax are non-enumerable too, while class fields are ordinary enumerable own properties.
configurable
A non-configurable property can't be deleted, can't switch between data and accessor, and most of its attributes become permanent:
const settings = {};
Object.defineProperty(settings, "mode", { value: "safe", writable: true, configurable: false });
console.log(delete settings.mode, settings.mode); // delete fails (false in sloppy mode)
Object.defineProperty(settings, "mode", { value: "fast" }); // allowed: still writable
Object.defineProperty(settings, "mode", { writable: false }); // allowed: true -> false only
console.log(settings.mode, Object.getOwnPropertyDescriptor(settings, "mode").writable);
try {
Object.defineProperty(settings, "mode", { writable: true }); // false -> true is not allowed
} catch (err) {
console.log(err.name);
}
false safe
fast false
TypeError
The precise rule: for a non-configurable property, you can still change value if it's writable, and you can change writable from true to false, but nothing else. Changing it back, toggling enumerable, or deleting the property all fail (with a TypeError from defineProperty, and delete returns false in sloppy mode or throws in strict mode).
Math.PI, Number.MAX_SAFE_INTEGER and similar constants are writable: false, configurable: false, which is why they can never change.
Accessor properties with defineProperty
You can also define getters and setters (article 53) through descriptors, useful when adding them to an existing object:
const temp = { celsius: 20 };
Object.defineProperty(temp, "fahrenheit", {
get() {
return (this.celsius * 9) / 5 + 32;
},
set(f) {
this.celsius = ((f - 32) * 5) / 9;
},
enumerable: true,
});
temp.fahrenheit = 212;
console.log(temp.celsius, temp.fahrenheit, Object.keys(temp));
100 212 [ 'celsius', 'fahrenheit' ]
A descriptor can't mix the two kinds: { value: 1, get() {} } throws a TypeError. An accessor with only get is read-only (assignments are ignored, or throw in strict mode).
Copying properties faithfully
Spread and Object.assign read each property's value. A getter is called, and the copy gets a plain data property holding its current result. To copy properties with their attributes intact, including getters, setters and non-enumerable ones, copy the descriptors:
const source = {
get timestamp() {
return "computed on access";
},
};
const spread = { ...source };
const exact = Object.defineProperties({}, Object.getOwnPropertyDescriptors(source));
console.log(typeof Object.getOwnPropertyDescriptor(spread, "timestamp").get);
console.log(typeof Object.getOwnPropertyDescriptor(exact, "timestamp").get);
undefined
function
For an exact shallow clone that keeps the prototype too:
const clone = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
Object.create(proto, descriptors) takes the same descriptor map as defineProperties.
Practical uses
- Constants on objects:
Object.defineProperty(api, "VERSION", { value: "2.1", enumerable: true }). - Hidden metadata: attach caches or IDs that shouldn't appear in JSON or loops. (Symbols and private class fields,
#cache, are often cleaner today.) - Lazy properties: a getter that computes once, then replaces itself with a plain data property:
const report = {
get summary() {
console.log("(computing summary)");
const value = "42 orders, 3 refunds";
Object.defineProperty(this, "summary", { value, enumerable: true });
return value;
},
};
console.log(report.summary);
console.log(report.summary);
(computing summary)
42 orders, 3 refunds
42 orders, 3 refunds
- Polyfills: add missing built-in methods as non-enumerable properties, so they don't show up in
for...inloops over arrays or objects.
Common questions
Why didn't my assignment change the property, and why was there no error?
The property is non-writable (or a getter without a setter), and the code isn't in strict mode, so the assignment was silently ignored. In modules, classes and "use strict" code it throws a TypeError.
Why are properties I added with defineProperty missing from Object.keys and JSON?
enumerable defaults to false with defineProperty. Pass enumerable: true (and usually writable: true, configurable: true) if you want it to behave like a normal property.
How do I list every own key, including hidden and symbol ones?
Reflect.ownKeys(obj) returns all own string and symbol keys, enumerable or not. Object.getOwnPropertyNames returns string keys only.
Is a non-configurable property also read-only?
Not necessarily. configurable: false prevents deleting it and changing its attributes, but if it's still writable: true, its value can change.
Summary
- Properties are either data properties (
value,writable) or accessors (get,set); both haveenumerableandconfigurable. - Assignment and literals set all flags to
true;definePropertydefaults omitted flags tofalse. writable: falseblocks assignment (silently, or aTypeErrorin strict mode), even through inheritance.enumerable: falsehides a property fromfor...in,Object.keys, JSON, spread andObject.assign.configurable: falsemakes the property permanent; only value changes (if writable) andwritable: true → falseremain possible.- Copy with
Object.defineProperties({}, Object.getOwnPropertyDescriptors(src))to preserve getters, setters and attributes.
Practice Questions
What does this print?
const o = {};
Object.defineProperty(o, "a", { value: 1 });
o.a = 2;
o.b = 3;
console.log(o.a, Object.keys(o));
Answer
1 [ 'b' ]. a is non-writable and non-enumerable (the defineProperty defaults), so the assignment is ignored in sloppy mode and a is hidden from Object.keys. b was created by assignment, so it's a normal property.
Make user.id read-only but still visible in Object.keys and JSON.stringify.
Answer
Object.defineProperty(user, "id", { writable: false });
When you redefine an existing property, the attributes you leave out keep their current values. So enumerable stays true, and only writable changes. To define the property from scratch instead:
Object.defineProperty(user, "id", { value: 42, enumerable: true });
Why does this throw?
Object.defineProperty({}, "x", { value: 1, get() { return 2; } });
Answer
A descriptor must be either a data descriptor (value/writable) or an accessor descriptor (get/set), never both, so defineProperty throws a TypeError.
After const copy = { ...source }, the copied total getter no longer updates. Why, and how do you fix it?
Answer
Spread reads source.total once, calling the getter, and stores the result as a plain data property. Copy the descriptors instead:
const copy = Object.defineProperties({}, Object.getOwnPropertyDescriptors(source));
Previous: Explicit Resource Management in JavaScript: using, await using and DisposableStack

More in JavaScript Course
Quiz: Modern JavaScript, ES2022 to ES2027 Features
Twenty-four questions on what's new in JavaScript: change-array-by-copy methods, groupBy, Set methods, iterator helpers, Promise.withResolvers and Promise.try, RegExp.escape and the v flag, using declarations, Error.isError, Base64 typed arrays, Temporal, getOrInsert and Iterator.zip.
Quiz: JavaScript's Delicate Details (Spec-Level Edge Cases)
Twenty-eight questions on the fine print of JavaScript, drawn from the ECMAScript specification and MDN: sparse arrays, -0, number limits, JSON edge cases, Date overflow, UTF-16, property attributes, inherited accessors, comparison quirks, labels, regex state and Symbol.toPrimitive.
Quiz: JavaScript Interview Questions (Output Prediction and Core Concepts)
Thirty classic JavaScript interview questions: closures in loops, hoisting and the TDZ, this, the event loop and microtasks, async/await, coercion, sorting and parseInt traps, prototypes, copying, finally, generators and bind. Every answer was verified by running it.
Quiz: JavaScript Course Final Exam (All 17 Parts)
The final exam for the JavaScript course: thirty questions spanning all seventeen parts, from syntax, types and operators through objects, arrays, functions, classes, modules, the standard library, iterators, async code, metaprogramming, browsers, Node.js and tooling.
Discussion
Sign in to ask a question or share what you learned.