![]() |
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 inspecting and manipulating object properties. These are the following topics that we are going to discuss: Table of Content hat is Object.keys()?The Object.keys() is a method that returns an array of the given object’s own enumerable property names in the same order as that provided by the for…in loop. Characteristics:
Applications:
Syntax:Object.keys(obj); Example: This example shows the use of Object.keys().
Output [ 'name', 'age', 'isStudent' ] What is Object.getOwnPropertyNames()?The Object.getOwnPropertyNames() is a method that returns an array of the all properties found directly in the given object. Characteristics:
Applications:
Syntax:Object.getOwnPropertyNames(obj); Example: This example shows the use of Object.getOwnPropertyNames().
Output [ 'name', 'age', 'isStudent', 'gender' ] Difference Between Object.keys() and Object.getOwnPropertyNames()
ConclusionUnderstanding the differences between the Object.keys() and Object.getOwnPropertyNames() is crucial for the effectively managing and manipulating object properties in JavaScript. While Object.keys() is typically used for the iterating over an object’s enumerable properties Object.getOwnPropertyNames() is used when a complete list of the object’s properties including the non-enumerable ones is needed. By choosing the appropriate method based on the requirements we can efficiently handle object properties in the JavaScript applications. |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 20 |