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

← All JS Object methods · Array methods · Spread vs rest