Open In App

JavaScript Object getOwnPropertyDescriptor() Method

Last Updated : 26 May, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

Become an expert in solving problems with DSA JavaScript—the course designed to teach you Data Structures and Algorithms using JavaScript. Over the next 90 days, dive deep into key DSA concepts, learn to optimize your code, and gain hands-on experience solving challenging problems. Whether you're a beginner or looking to refine your JavaScript skills, this course will provide the knowledge you need to succeed.
Take on the Three 90 Challenge today! Complete 90% of the course within 90 days, and you’ll earn a 90% refund. This challenge is your chance to stay motivated, improve your skills, and get rewarded for your progress. Don’t wait—start your journey to DSA mastery with JavaScript today!


Next Article

Similar Reads

JavaScript Reflect getOwnPropertyDescriptor() Method
JavaScript Reflect.getOwnPropertyDescriptor() method in Javascript is used to get the descriptor of an object if it exists in the object. It is the same as the Object.getOwnPropertyDescriptor method, but non-object targets are handled differently. Syntax: Reflect.getOwnPropertyDescriptor(obj, Key) Parameters: This method accepts two parameters as m
2 min read
JavaScript Handler getOwnPropertyDescriptor() Method
JavaScript handler.getOwnPropertyDescriptor() method in Javascript is a trap for the Object.getOwnPropertyDescriptor() method. A property cannot be reported as non-existent if it exists as a non-configurable own property of the target object. Syntax: const p = new Proxy(target, { getOwnPropertyDescriptor: function(target, prop) { } }); Parameters:
2 min read
How to access an object having spaces in the object's key using JavaScript ?
Given an object and the task is to access the object in which the key contains spaces. There are a few methods to solve this problem which are discussed below: Approach 1: Create an object having space-separated keys.Use square bracket notation instead of dot notation to access the property. Example: This example implements the above approach. C/C+
2 min read
How to check if the provided value is an object created by the Object constructor in JavaScript ?
In this article, we will learn how to check if the provided value is an object created by the Object constructor in JavaScript. Almost all the values in JavaScript are objects except primitive values. There are several methods that can be used to check if the provided value is an object created by the Object constructor in JavaScript. Using the ins
3 min read
How to create a new object from the specified object, where all the keys are in lowercase in JavaScript?
In this article, we will learn how to make a new object from the specified object where all the keys are in lowercase using JavaScript. Example: Here, we have converted the upper case to lower case key values. Input: {RollNo : 1, Mark : 78} Output: {rollno : 1, mark : 78} Approach 1: A simple approach is to Extract keys from Object and LowerCase to
2 min read
How Check if object value exists not add a new object to array using JavaScript ?
In this tutorial, we need to check whether an object exists within a JavaScript array of objects and if they are not present then we need to add a new object to the array. We can say, every variable is an object, in Javascript. For example, if we have an array of objects like the following. obj = { id: 1, name: ''Geeks1" }, { id: 2, name: '"Geeks2"
3 min read
How to Search Character in List of Array Object Inside an Array Object in JavaScript ?
Searching for a character in a list of array objects inside an array object involves inspecting each array object, and then examining each array within for the presence of the specified character. The goal is to determine if the character exists within any of the nested arrays. There are several ways to search for characters in a list of array obje
4 min read
Difference between Object.keys() and Object.entries() methods in JavaScript
Object.keys() and Object.entries() are methods in JavaScript used to iterate over the properties of an object. They differ in how they provide access to object properties: Object.keys() returns an array of a given object's own enumerable property names, while Object.entries() returns an array of a given object's own enumerable string-keyed property
2 min read
How to compare two objects to determine the first object contains equivalent property values to the second object in JavaScript ?
In this article, we are going to learn about comparing two objects to determine if the first object contains equivalent property values to the second object, In JavaScript, comparing the values of two objects involves checking if they have the same properties with corresponding values. Given two objects obj1 and obj2 and the task are to check that
6 min read
How to Append an Object as a Key Value in an Existing Object in JavaScript ?
In JavaScript, An object is a key-value pair structure. The key represents the property of the object and the value represents the associated value of the property. In JavaScript objects, we can also append a new object as a Key-value pair in an existing object in various ways which are as follows. Table of Content Using JavaScript Spread (...) Ope
3 min read
How to Add Duplicate Object Key with Different Value to Another Object in an Array in JavaScript ?
Adding duplicate object keys with different values to another object in an array in JavaScript refers to aggregating values under the same key from multiple objects in an array, creating a new object where each key corresponds to an array of associated values. Table of Content Using for...of LoopUsing reduce()Using a Map for Multiple ValuesUsing fo
4 min read
Difference Between Object.keys() and Object.getOwnPropertyNames() in JavaScript
In JavaScript, Object.keys() and Object.getOwnPropertyNames() both retrieve properties of an object but differ in scope. Object.keys() returns an array of an object's own enumerable property names. In contrast, Object.getOwnPropertyNames() returns an array of all own property names, including non-enumerable properties. These methods are useful for
3 min read
How to Push an Object into Another Object in JavaScript ?
In JavaScript, the concept of pushing an object into another object involves embedding one object within another as a property. This technique is useful for organizing data structures where related information is grouped, enhancing the ability to manage and manipulate complex data sets effectively. There are several ways to push an object into anot
4 min read
How to check a JavaScript Object is a DOM Object ?
Checking if a JavaScript object is a DOM object involves verifying whether the object represents an element or component within the Document Object Model (DOM) of an HTML or XML document. This can be done by checking if the object is an instance of Node, Element, or other specific DOM interfaces. What is DOM (Document Object Model)?The DOM (Documen
2 min read
How will you access the reference to same object within the object in PHP ?
In this article, we will see how we can access the reference to the same object within that object in PHP. To do that we have to use the "$this" keyword provided by PHP. PHP $this Keyword: It is used to refer to the current objectIt can only be used inside the internal methods of the class.$this keyword can refer to another object only if it is ins
2 min read
HTML | DOM Object Object
The Object object represents only HTML <object> element. We can access any <object> element by using the getElementById(); and also can create object element by using createElement(); method.Syntax: It is used to access Object element document.getElementById("id"); It is used to create object element document.createElement("object"); Pr
2 min read
How to set an object key inside a state object in React Hooks?
We can update a React hooks state object that has a nested object containing objects with index keys with the following approach, Before doing so, consider the following example: Example: Following is the default state object: const [data, setData] = useState({ name:'', contact:'', address:{ 0:{}, 1:{}, } }) Following is the desired output after up
2 min read
How to use array that include and check an object against a property of an object ?
Array.includes() Method: In JavaScript, includes() method is used to determine that a particular element is present in an array or not. It returns true if the element is present and false when it is absent. Syntax: array_name.includes(searchElement, ?fromIndex) Parameters: searchElement: The element to be search in the array.fromIndex: The index fr
3 min read
What is a potential pitfall using typeof bar === "object" to determine if bar is an object ?
The typeof operator in JavaScript returns a string that indicates the data type of the operand, whether it be a variable, function, or object. Syntax: Following is the syntax of typeof operator: typeof operand // OR typeof(operand)Parameter: It takes the following parameter: operand: The expression whose type needs to be evaluated.Example: Basic ex
4 min read
Difference between Object.values and Object.entries Methods
The object is the parent class from which all the javascript objects are inherited and these two methods are the static methods of the Object class as they are called by the class name of the Object class. JavaScript Object.values() Method In the same order as a for...in the loop, the Object.values() method returns an array of the enumerable proper
2 min read
Object is undefined" error when trying to rotate object in three.js
In this article, we will see the "Uncaught TypeError: Cannot read properties of undefined (reading 'rotation') at animate". It is a type error for a variable, in Three .js, this error occurs when the variable is declared, but the value is not assigned or the value is undefined. We can observe from the error, message that this error occurred, when t
3 min read
How to select first object in object in AngularJS?
The main problem that we are dealing with is that for an object of objects reading the object of a particular index position is not as simple as a list. We cannot loop over it using ngFor as an object is not considered an iterable. The importance of this issue may arise when the data received from any source is an object containing objects(like JSO
3 min read
How to Replace an Object in an Array with Another Object Based on Property ?
In JavaScript, replacing an object in an array with another object based on a specific property involves identifying and updating elements within the array. This task is essential when modifications to individual objects are required, such as updating values or swapping objects based on a particular property criteria. Table of Content Using map()Us
3 min read
How to Remove Multiple Object from Nested Object Array ?
Removing multiple objects from a nested object array in JavaScript involves traversing the array and filtering out the objects that meet certain criteria, such as having specific properties or values. This can be achieved using various array manipulation methods available in JavaScript. Below are the approaches used to remove the multiple object fr
3 min read
How to Replace an Object in an Array with Another Object Based on Property ?
In JavaScript, an array is a data structure that can hold a collection of values, which can be of any data type, including numbers, strings, and objects. When an array contains objects, it is called an array of objects. Table of Content Using findIndex and splice methodsUsing filter and concat methodsUsing the map methodUsing findIndex and splice m
3 min read
How to Initialize a TypeScript Object with a JSON-Object ?
To initialize a TypeScript Object with a JSON-Object, we have multiple approaches. In this article, we are going to learn how to initialize a TypeScript Object with a JSON-Object. Below are the approaches used to initialize a TypeScript Object with a JSON-Object: Table of Content Object.assign Type AssertionSpread OperatorClass InitializationUsing
3 min read
How to Convert a JavaScript Object to a Form Data in JavaScript?
In web development, especially when dealing with forms and AJAX requests, you might find yourself needing to convert a JavaScript object into a FormData object. The FormData interface provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be sent via XMLHttpRequest or fetch() to the ser
4 min read
JavaScript Object.prototype.__lookupGetter__() Method
JavaScript Object.prototype.__lookupGetter__() method returns a function that is bound to the specified property as a getter. This method is although not recommended using and it is now deprecated. Syntax: __lookupGetter__(sprop) Parameters: sprop is a string representing the name of the property whose getter would be returned. Return Value: It ret
1 min read
JavaScript Object seal() Method
JavaScript Object.seal() method is used to seal an object. Sealing an object does not allow new properties to be added and marks all existing properties as non-configurable. Although values of present properties can be changed as long as they are writable. The object to be sealed is passed as an argument and the method returns the object which has
3 min read
JavaScript Object isFrozen() Method
Object.isFrozen() Method: Among the Object constructor methods, there is a method Object.isFrozen() which is used to determine if an object is frozen or not. An object is frozen if all of the below-mentioned conditions hold true : If it is not extensible.If all of its properties are non-configurable.If all its data properties are non-writable. Obje
3 min read
  翻译: