Difference Between Object.keys() and Object.getOwnPropertyNames() in JavaScript
Last Updated :
23 Jul, 2025
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:
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:
- The Returns an array of the strings representing the object’s own enumerable properties.
- Does not include the non-enumerable properties.
- The Only includes properties that are directly on the object not those in the prototype chain.
Applications:
- Useful for the iterating over an object's properties.
- Commonly used when the need is to work with the only enumerable properties.
Syntax:
Object.keys(obj);
Example: This example shows the use of Object.keys().
JavaScript
const person = {
name: 'Alice',
age: 30,
isStudent: false
};
const keys = Object.keys(person);
console.log(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:
- The Returns an array of the strings corresponding to the properties found directly in the given object.
- Includes both the enumerable and non-enumerable properties.
- Only includes properties that are directly on the object, not those in the prototype chain.
Applications:
- Useful for the getting all properties of the object regardless of their enumerability.
- Often used in scenarios where complete information about an object's properties is required.
Syntax:
Object.getOwnPropertyNames(obj);
Example: This example shows the use of Object.getOwnPropertyNames().
JavaScript
const person = {
name: 'Alice',
age: 30,
isStudent: false
};
Object.defineProperty(person, 'gender', {
value: 'female',
enumerable: false
});
const propertyNames = Object.getOwnPropertyNames(person);
console.log(propertyNames);
Output[ 'name', 'age', 'isStudent', 'gender' ]
Difference Between Object.keys() and Object.getOwnPropertyNames()
Characteristics | Object.keys() | Object.getOwnPropertyNames() |
---|
Property Type | Only enumerable properties | Both the enumerable and non-enumerable |
---|
Prototype Chain Properties | No | No |
---|
Symbol Properties | No | No |
---|
Returns | Array of strings | Array of strings |
---|
Typical Use Case | Iterating over own enumerable the properties | Retrieving the all own properties |
---|
Conclusion
Understanding 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.