JavaScript Object getOwnPropertyDescriptor() Method
Last Updated :
26 May, 2023
The Object.getOwnPropertyDescriptor() method in JavaScript is a standard built-in object that enables the full information on a property to be accessed and returns a property descriptor for the own property of a given object.
Syntax:
Object.getOwnPropertyDescriptor( obj, prop )
Parameters: This method accepts two parameters as mentioned above and described below:
- obj: This parameter holds the object in which the property is to be looked.
- 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: In this example, we will check if an object contains some property or not using the Object.getOwnPropertyDescriptor() method in JavaScript.
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: In this example, we will check if an object contains some property or not using the Object.getOwnPropertyDescriptor() method in JavaScript.
javascript
let 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 }
We have a complete list of Javascript Object methods, to check those please go through this JavaScript Object Complete Reference article.
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
Similar Reads
JavaScript Constructor Method A constructor in JavaScript is a special function used to create and initialize objects. It sets up object properties and is typically invoked using the new keyword. Constructors allow for the creation of multiple instances with similar properties and methods.In JavaScript, constructors can be defin
7 min read
JavaScript Object assign() Method The Object.assign() method is used to copy the values and properties from one or more source objects to a target object. It invokes getters and setters since it uses both [[Get]] on the source and [[Set]] on the target.Syntax:Object.assign(target, ...sources);Parameters:target: It is the target obje
4 min read
JavaScript Object create() Method JavaScript object.create() method is used to create a new object with the specified prototype object and properties. Object.create() method returns a new object with the specified prototype object and properties.Syntax:Object.create(prototype[, propertiesObject])Parameters:prototype: It is the proto
3 min read
JavaScript Object defineProperty() Method The Object.defineProperty() method in JavaScript is a Standard built-in object which defines a new property directly on an object or it can also modify the existing property of an object and return the object. Syntax:Object.defineProperty(obj, prop, descriptor)Parameters:This method accepts three pa
3 min read
JavaScript Object defineProperties() Method The Object.defineProperties() method in JavaScript is a standard built-in Object that defines a new or modifies existing properties directly on an object and it returns the object.Syntax:Object.defineProperties(obj, props) Parameters:Obj: This parameter holds the object on which the properties are g
2 min read
JavaScript Object entries() Method The Object.entries() method in JavaScript is used to retrieve an array of an object's enumerable property [key, value] pairs. This method is particularly useful for transforming and iterating over objects in situations where array-like manipulation is needed.Syntax:Object.entries(obj);Parameters:obj
4 min read
JavaScript Object freeze() Method The Object.freeze() method is used to freeze an object. Freezing an object does not allow new properties to be added to the object and prevents removing or altering the existing properties. Object.freeze() preserves the enumerability, Configurability, writability, and prototype of the object. It ret
3 min read
JavaScript Object getOwnPropertyDescriptor() Method The Object.getOwnPropertyDescriptor() method in JavaScript is a standard built-in object that enables the full information on a property to be accessed and returns a property descriptor for the own property of a given object. Syntax: Object.getOwnPropertyDescriptor( obj, prop ) Parameters: This meth
2 min read
JavaScript Object getOwnPropertyNames() Method The Object.getOwnPropertyNames() method in JavaScript is a standard built-in object which returns all properties that are present in a given object except for those symbol-based non-enumerable properties.Syntax:Object.getOwnPropertyNames(obj)Parameters:This method accepts a single parameter as menti
3 min read
JavaScript Object getOwnPropertySymbols() Method The Object.getOwnPropertySymbols() method in JavaScript is a standard built-in object which returns an array of all symbol properties that are present in a given object. An empty array is returned until symbol properties are set on the object. Syntax: Object.getOwnPropertySymbols(obj) Parameters: ob
2 min read