JavaScript Object getOwnPropertyNames method usage examples

This blog post explains example of the following things.

  1. getOwnPropertyNames in object syntax and examples
  2. Difference between Object.getOwnPropertyNames() vs Object.keys() methods.
  3. Convert the Object keys into array sort order.
  4. List out all functions of an object Length of an object

getOwnPropertyNames method in an object class

getOwnPropertyNames() is a inbuilt method in Object class in javascript.

Every type in javascript extends object, Array, a map that extends Object has getOwnpropertynames method.

An object has enumerable and non-enumerable properties. Properties are attributes, and functions.

Syntax:

Object.getOwnPropertyNames(Object);

Input type - Accepts an object as an input parameter Returns an array of all enumerable properties, not-enumerable properties, Please check Object properties guide. Let’s see an example for Enumerable keys display an object.

const employee = { name: "Franc", department: "sales" };
console.log(Object.getOwnPropertyNames(employee));
["name", "department"];

Let’s add non enumerable property to an existin object using Object.defineProperty() method.

Object.defineProperty(employee,"address",{country:"India",enumerable:false})

getOwnPropertyNames() method prints the array of enumerable and non-enumerable properties
console.log(Object.getOwnPropertyNames(employee));
[ 'name', 'department', 'address' ]

Difference between Object.getOwnPropertyNames() vs Object.keys() methods

Object.keys() returns the array of enumerable properties keys only Object.getOwnPropertyNames() returns the array of enumerable and non-enumerable properties keys.

const employee = { name: "Franc", department: "sales" };
Object.defineProperty(employee, "address", {
  country: "India",
  enumerable: false,
});
console.log(Object.keys(employee));

console.log(Object.getOwnPropertyNames(employee));

Output is

["name", "department"][("name", "department", "address")];

Convert the Object keys into array sort order

getOwnPropertyNames() returns array of keys. pass returned array with sort or reverse method for sorting and returns array sorted as a seen example.

console.log(Object.getOwnPropertyNames(employee));
console.log(Object.getOwnPropertyNames(employee).sort());

output is

["name", "department", "address"][("address", "department", "name")];

Let’s see an example object to array with values by sorting key order.

Object.getOwnPropertyNames(employee)
  .sort()
  .forEach(function (element) {
    console.log(employee[element]);
  });

List out all functions of an object

An array is an object, This example prints out all functions or methods of an array using the getOwnPropertyNames() method

console.log(Object.getOwnPropertyNames(Array));
//[ 'length', 'name', 'prototype', 'isArray', 'from', 'of' ]

console.log(
  Object.getOwnPropertyNames(Array).filter(function (key) {
    return typeof Math[key] === "function";
  }),
);
//[ 'isArray', 'from', 'of' ]

Length of properties of an object

getOwnPropertyNames() returns an array of properties of an object, calling length on an object gives the length of an array.

console.log(Object.getOwnPropertyNames(employee).length); //3