.getOwnPropertyNames()
Published Dec 16, 2023
Contribute to Docs
The .getOwnPropertyNames()
method returns all properties that are present in a given object except for those symbol-based non-enumerable properties.
Syntax
Object.getOwnPropertyNames(obj)
obj
: This parameter holds the object whose enumerable and non-enumerable properties are to be returned.
Examples
Example 1
In this example, Object.getOwnPropertyNames()
returns an array containing the property names of the person
object:
const person = {firstName: 'Jane',lastName: 'Doe',age: 40,};const propertyNames = Object.getOwnPropertyNames(person);console.log(propertyNames);
The above example will return the following output:
['firstName', 'lastName', 'age']
Example 2
In this example, the obj
object has both enumerable and non-enumerable properties. Object.getOwnPropertyNames()
includes both types of properties in the resulting array:
const obj = Object.create({},{nonEnumerableProp: {value: 'I am non-enumerable',enumerable: false,},enumerableProp: {value: 'I am enumerable',enumerable: true,},});const propertyNames = Object.getOwnPropertyNames(obj);console.log(propertyNames);
The above example will return the following output:
['nonEnumerableProp', 'enumerableProp']
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.