
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to get Property Descriptors of an Object in JavaScript?
In this article, we are going to explore property descriptors and how we can fetch these values from an object. The Object.getOwnPropertyDescriptor method returns an object that describes the specific property from the given object. A JavaScript object can be created in multiple ways that can be called by using the property descriptors from the object.
Syntax
Object.getOwnPropertyDescriptor(obj, prop
The property descriptor takes two parameters as inputs that are described below −
obj − The object refers to the object name whose properties need to be described.
prop − It defines the specific property from the object whose value needs to be returned.
This method returns the property of an object if it exists.
Example 1
In the below example, we have created a simple object. After creating the object, we are using the property descriptors to find out the specified value for the property. We will use the Object.getOwnPropertyDescriptor() method to return the attributes and values related to the property.
# index.html
<!DOCTYPE html> <html lang="en"> <head> <title>Property Descriptors</title> </head> <body> <h1 style="color: green;"> Welcome To Tutorials Point </h1> <script> // Object const Obj = { property1: "Tutorials Point", property2: "Simply Easy Learning" }; const descriptor1 = Object.getOwnPropertyDescriptor(Obj, 'property1'); const descriptor2 = Object.getOwnPropertyDescriptor(Obj, 'property2'); console.log(descriptor1.configurable); // expected output: true console.log(descriptor1.enumerable); // expected output: true console.log(descriptor1.value); // expected output: Tutorials Point console.log(descriptor2.value); // expected output: Simply Easy Learning </script> </body> </html>
Output
On successful execution, the result can be found in the console.
Descriptors
A property descriptor of an object uses the following attributes to define each property.
value − This returns the value associated with the property attribute.
writable − This indicates whether the property has changed or not. It will return true if the property is changed.
enumerable − This returns true if the property is visible during enumeration of the corresponding object.
configurable − This indicates if the property can be configured or not.
Example 2
# index.html
<!DOCTYPE html> <html lang="en"> <head> <title>Property Descriptors</title> </head> <body> <h1 style="color: green;"> Welcome To Tutorials Point </h1> <script> // Object const Obj = { property1: "Tutorials Point", property2: "Simply Easy Learning" }; const descriptor1 = Object.getOwnPropertyDescriptor(Obj, 'property1'); const descriptor2 = Object.getOwnPropertyDescriptor(Obj, 'property2'); console.log(descriptor1); console.log(descriptor1); </script> </body> </html>
Output
- Related Articles
- How to delete a property of an object in JavaScript?
- How to get the length of an object in JavaScript?
- How to get the values of an object in JavaScript?
- How to set dynamic property keys to an object in JavaScript?
- How to get an object containing parameters of current URL in JavaScript?
- How to Declare an Object with Computed Property Name in JavaScript?
- How to create an object property from a variable value in JavaScript?
- How to return an array whose elements are the enumerable property values of an object in JavaScript?
- How to Get Current Value of a CSS Property in JavaScript?
- How to get the size of a json object in JavaScript?
- Can we assign new property to an object using deconstruction in JavaScript?
- How to invert an object in JavaScript?
- How to freeze an Object in JavaScript?
- How to clone an object in JavaScript?
- Iterate over an object and remove false property in JavaScript
