inspection
Object.getOwnPropertyDescriptor
Returns the property descriptor for an own property on the given object, or undefined if the property does not exist. Descriptors describe writability, enumerability, configurability, and getters/setters.
Object.getOwnPropertyDescriptor(obj, prop) Parameters
| Parameter | Purpose |
|---|---|
| obj | The target object |
| prop | The property key whose descriptor to retrieve |
| returns | Descriptor object or undefined |
Examples
console.log(Object.getOwnPropertyDescriptor({ a: 1 }, 'a')); Logs { value: 1, writable: true, enumerable: true, configurable: true }
const o = {};
Object.defineProperty(o, 'x', { value: 1 });
console.log(Object.getOwnPropertyDescriptor(o, 'x')); Logs { value: 1, writable: false, enumerable: false, configurable: false }
const o = { get name() { return 'Ada'; } };
console.log(Object.getOwnPropertyDescriptor(o, 'name')); Logs an accessor descriptor with get/set functions
console.log(Object.getOwnPropertyDescriptor({}, 'missing')); Logs undefined
Gotcha
Only inspects OWN properties — returns undefined for inherited ones. For accessor properties you get { get, set, enumerable, configurable }; for data properties you get { value, writable, enumerable, configurable }.
Related methods
Object.defineProperty
Defines a new property directly on an object, or modifies an existing one, with fine-grained control over its descriptor. Returns the object.
Object.getOwnPropertyNames
Returns an array of all own string-keyed property names, including non-enumerable ones (but not Symbol keys). Useful for introspection where Object.keys is too restrictive.
Object.hasOwn
Returns true if the specified object has the given property as its own property (not inherited). Added in ES2022 as the recommended replacement for Object.prototype.hasOwnProperty.
Object.keys
Returns an array of a given object's own enumerable string-keyed property names. The order matches a normal for...in loop but excludes inherited and non-enumerable properties.
Object.values
Returns an array of a given object's own enumerable string-keyed property values. Introduced in ES2017 as a companion to Object.keys.