Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript | Object.getOwnPropertyDescriptor() Method

  • Last Updated : 17 Sep, 2021

The Object.getOwnPropertyDescriptor() method in JavaScript is standard built-in objects that returns a property descriptor for an own property of a given object.
Syntax: 
 

Object.getOwnPropertyDescriptor( obj, prop )

Parameters: This method accept two parameters as mentioned above and described below: 
 

  • obj: This parameter holds the object in which the property are to be look.
  • prop: This parameter holds the name or Symbol of the property whose description is to be retrieved.

Return value: This method returns a property descriptor of the given property or undefined depending upon the existence of the object.
Below examples illustrate the Object.getOwnPropertyDescriptor() method in JavaScript:
Example 1: 
 

javascript




const geeks1 = { 
  prop1: "GeeksforGeeks" 
const geeks2 = { 
  prop2: "Best Platform" 
const geeks3 = { 
  prop3: "And Computer science portal" 
}
const descriptor1 = Object.getOwnPropertyDescriptor(geeks1, 'prop1'); 
const descriptor2 = Object.getOwnPropertyDescriptor(geeks2, 'prop2'); 
const descriptor3 = Object.getOwnPropertyDescriptor(geeks3, 'prop3'); 
console.log(descriptor1.enumerable); 
console.log(descriptor2.enumerable); 
console.log(descriptor1.value); 
console.log(descriptor2.value); 
console.log(descriptor3.enumerable); 
console.log(descriptor3.value); 

Output: 
 

true
true
"GeeksforGeeks"
"Best Platform"
true
"And Computer science portal"

Example 2: 
 

javascript




var geek, result;
geek = { get foo() { return 17; } };
d = Object.getOwnPropertyDescriptor(geek, 'foo');
console.log(d)
 
geek = { bar: 42 };
d = Object.getOwnPropertyDescriptor(geek, 'bar');
console.log(d)
 
geek = { [Symbol.for('baz')]: 73 }
d = Object.getOwnPropertyDescriptor(geek, Symbol.for('baz'));
console.log(d)
 
geek = {};
Object.defineProperty(geek, 'qux', {
  value: 8675309,
  writable: false,
  enumerable: false
});
d = Object.getOwnPropertyDescriptor(geek, 'qux');
console.log(d)

Output: 
 

Object { get: get foo() { return 17; }, set: undefined, enumerable: true, configurable: true }
Object { value: 42, writable: true, enumerable: true, configurable: true }
Object { value: 73, writable: true, enumerable: true, configurable: true }
Object { value: 8675309, writable: false, enumerable: false, configurable: false }

Supported Browsers: The browsers supported by Object.getOwnPropertyDescriptor() method are listed below: 
 

  • Google Chrome 5.0 and above
  • Internet Explorer 9.0 and above
  • Mozilla 4.0 and above
  • Opera 12 and above
  • Safari 5.0 and above

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!