Open In App

JavaScript Object.getOwnPropertyDescriptors() Method

Last Updated : 27 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Object.getOwnPropertyDescriptors() method in JavaScript is a standard built-in object which returns all property descriptors of a given object. 

Note: GetOwnPropertyDescriptors() ignores symbolic properties, as compared to getOwnPropertyDescriptor() which is the difference between the two methods.

Syntax:

Object.getOwnPropertyDescriptors( obj )

Parameters: This method accepts a single parameter as mentioned above and described below:

  • obj: This parameter holds the object for which all own property descriptors are to get.

Return value: This method returns an object containing all its own property descriptors of an object. This method may return an empty object for the object having no properties. 

The Below examples illustrate the Object.getOwnPropertyDescriptors() method in JavaScript: 

Example 1: In this example, we will see the basic use of the Object.getOwnPropertyDescriptors() method in JavaScript.

javascript




<script>
    const geeks1 = {
    prop1: "GeeksforGeeks"
    }
    const geeks2 = {
    prop2: "Best Platform",
    prop3: "And Computer science portal"
    }
    const descriptor1 = Object.getOwnPropertyDescriptors(geeks1);
    const descriptor2 = Object.getOwnPropertyDescriptors(geeks2);
    console.log(descriptor1.prop1.enumerable);
    console.log(descriptor2.prop2.enumerable);
    console.log(descriptor1.prop1.value);
    console.log(descriptor2.prop2.value);
    console.log(descriptor2.prop3.value);
</script>


Output:

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

Example 2: In this example, we will see the basic use of the Object.getOwnPropertyDescriptors() method in JavaScript.

javascript




<script>
    const geeks1 = {
    prop1: 22
    };
    const descriptors1 =
        Object.getOwnPropertyDescriptors(geeks1);
      
    console.log(descriptors1.prop1.value);
    console.log(descriptors1.prop1);
    console.log(descriptors1.prop1.writable);
      
    const geeks2 = {
    prop2: " getOwnPropertyDescriptors"
    };
          
    const descriptors2 =
        Object.getOwnPropertyDescriptors(geeks2);
      
    console.log(descriptors2.prop2.writable);
    console.log(descriptors2.prop2.value);
</script>


Output:

22
Object { value: 22, writable: true, enumerable: true, configurable: true }
true
true
" getOwnPropertyDescriptors"

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.getOwnPropertyDescriptors() method are listed below:

  • Google Chrome 54 and above
  • Firefox 50 and above
  • Opera 41 and above
  • Safari 10 and above
  • Edge 15 and above


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads