JavaScript Object getOwnPropertyDescriptor() method
returns an object describing the configuration of a specific property on a given object

Object.getOwnPropertyDescriptor() static method returns an object describing the configuration of a specific property on a given object (that is, one directly present on an object and not in the object's prototype chain).

The object returned is mutable but mutating it has no effect on the original property's configuration.

syntax

Object.getOwnPropertyDescriptor(obj, prop)

obj The object in which to look for the property.

prop The name or Symbol of the property whose description is to be retrieved.

The return value will be the property descriptor of the given property if it exists on the object, undefined otherwise.

Object.getOwnPropertyDescriptor() method permits examination of the precise description of a property.

A property in JavaScript consists of either a string-valued name or a Symbol and a property descriptor.

A property descriptor is a record with some of the following attributes ↴

value: the value associated with the property (data descriptors only).

writable: set to true if and only if the value associated with the property may be changed (data descriptors only).

configurable: set to true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object.

enumerable: set to true if and only if this property shows up during enumeration of the properties on the corresponding object.

get: function which serves as a getter for the property, or undefined if there is no getter (accessor descriptors only).

set: function which serves as a setter for the property, or undefined if there is no setter (accessor descriptors only).

let employee = { name: 'Fred', age: 44 };

let descriptor = Object.getOwnPropertyDescriptor(employee, 'name');

descriptor; returns ↴

{value: 'Fred', writable: true, enumerable: true, configurable: true}

let descriptor = Object.getOwnPropertyDescriptor(employee, 'age');

descriptor; returns ↴

{value: 44, writable: true, enumerable: true, configurable: true}

We can also change these descriptors using Object.defineProperty(). This can be used to make properties read-only, hide them from enumeration, or prevent them from being deleted or reconfigured.

syntax description
return the property descriptor of the given property
Object.getOwnPropertyDescriptor(myObject, property)