The Object.getOwnPropertyNames()
method returns an array of all the properties found in a given object.
Example
const obj = {
name: 'Alexander',
age: 32,
address: 'Macedonia',
};
// find out the properties present in obj
const propertyNames = Object.getOwnPropertyNames(obj);
console.log(propertyNames);
// Output: [ 'name', 'age', 'address' ]
getOwnPropertyNames() Syntax
The syntax of the getOwnPropertyNames()
method is:
Object.getOwnPropertyNames(obj)
Here, getOwnPropertyNames()
is a static method. Hence, we need to access the method using the class name, Object
.
getOwnPropertyNames() Parameters
The getOwnPropertyNames()
method takes in:
- obj - the object whose properties are to be returned.
getOwnPropertyNames() Return Value
The getOwnPropertyNames()
method returns an array of strings corresponding to the properties in the given object.
Example 1: JavaScript Object.getOwnPropertyNames()
// create an object
const obj = {
name: "Jack",
address : "London",
age: 22,
}
// returns the array of properties of obj
let objProperties = Object.getOwnPropertyNames(obj);
console.log(objProperties);
// Output: [ 'name', 'address', 'age' ]
In the above example, we have used the getOwnPropertyNames()
with the obj object which returns an array including all property names or keys.
The result is stored in the objProperties object, which shows the specified array when logged to the console.
Example 2: getOwnPropertyNames() With Array-like Objects
// array-like object
let obj = { 75: "A", 67: "C", 66: "B" };
// get the property names of obj
console.log(Object.getOwnPropertyNames(obj));
// Output: [ '66', '67', '75' ]
In the above example, we have created an array-like object obj having random integers as the keys.
The getOwnPropertyNames()
method, when used with obj, returns an array of numeric strings in ascending order.
Example 3: getOwnPropertyNames() With Arrays
// create an array
let arr = ["a", "b", "c"];
// get the property names of arr
console.log(Object.getOwnPropertyNames(arr));
// Output: [ '0', '1', '2', 'length' ]
In the above example, we have used an array named arr with the getOwnPropertyNames()
to find the properties present in the array.
The output consists of indexes of the array along with the length property. The length property is the built-in property of the array in JavaScript that returns the number of elements in the array.
Example 4: getOwnPropertyNames() With Non-Enumerable Properties
// create an object
const obj = {
name: "Jack",
address : "London",
age: 22,
}
// defines the key girlFriend and
// sets enumerable as false
Object.defineProperty(obj, "girlFriend", {
value: "Jessica",
enumerable : false,
})
// prints the both enumerable and non-enumerable keys
console.log(Object.getOwnPropertyNames(obj));
// Output: [ 'name', 'address', 'age', 'girlFriend' ]
// print only the enumerable keys of obj
console.log(Object.keys(obj));
// Output: [ 'name', 'address', 'age' ]
In the above example, we first created an object obj with some initial properties.
We then added the girlFriend
property using the defineProperty() method and configured the enumerable to false
.
As we can see from the output, the Object.getOwnPropertyNames()
method returns both the enumerable and non-enumerable properties of obj.
On the other hand, the Object.keys() method only returns the enumerable properties.
Remember: Object.getOwnPropertyNames()
returns all the enumerable and non-enumerable properties of the object while Object.keys()
returns all the enumerable properties of the object.
Also Read: