JavaScript .getOwnPropertyDescriptor()
Published Dec 15, 2023
The .getOwnPropertyDescriptor() method is a built-in object that enables access to full information about a property and returns a property descriptor of the given property if it exists on the object, otherwise, it returns undefined. It only works on its properties directly defined by the object itself, meaning it does not have access to inherited properties from the object’s prototype chain.
Syntax
Here’s the basic syntax of Object.getOwnPropertyDescriptor():
Object.getOwnPropertyDescriptor(obj, prop)
obj: The object where to look for the property.prop: The name or symbol of the property whose description is to be retrieved.
Example
The example below shows how Object.getOwnPropertyDescriptor() works:
const person = {name: "Jane",age: 21};const descriptor = Object.getOwnPropertyDescriptor(person, 'name');console.log(descriptor)
This example results in the following output:
{ value: 'Jane', writable: true, enumerable: true, configurable: true }
Note:
Object.getOwnPropertyDescriptor()method only works for an individual property of an object.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours