Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Object

Object.getOwnPropertyDescriptor()

Reference for JavaScript Object.getOwnPropertyDescriptor(): read a property's value, writable, enumerable, configurable, get and set attributes, with examples.

Object.getOwnPropertyDescriptor() returns an object that describes one property of an object: its value (or getter and setter) and its three attributes writable, enumerable and configurable. It looks only at the object's own properties, not at the prototype chain.

Use it to inspect how a property is defined, to check whether it can be changed, or to copy a property exactly, including accessors, with Object.defineProperty().

JavaScript
Object.getOwnPropertyDescriptor(obj, "name");
// { value: "Ada", writable: true, enumerable: true, configurable: true }

Try it

JS Data and accessor properties

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Built-ins and frozen objects

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
Object.getOwnPropertyDescriptor(obj, prop)
Name Description
obj The object to inspect. Primitives are converted to objects.
prop The property key: a string or a symbol.

Return value: a descriptor object, or undefined if the object has no own property with that key.

A data property gives value, writable, enumerable and configurable. An accessor property gives get, set, enumerable and configurable.

Exceptions: a TypeError if obj is null or undefined.

Usage notes

  • Own properties only. "toString" on a plain object returns undefined, even though obj.toString works, because the method lives on the prototype.
  • The result is a fresh copy. Changing the returned descriptor does not change the property. Pass it to Object.defineProperty() for that.
  • Accessors report functions, not values. Reading the descriptor of a getter does not call the getter.
  • undefined as a value is not the same as missing. A property that exists with the value undefined returns a descriptor; a missing property returns undefined itself.
  • All at once: Object.getOwnPropertyDescriptors() returns descriptors for every own property, which is the basis of exact object cloning with prototypes.
  • Built-ins have their own settings: array length is writable but not enumerable or configurable, and Math.PI is not writable.

Browser support

Object.getOwnPropertyDescriptor() is available in every browser and JavaScript runtime.