How to get all property names from an object in JavaScript?

Published June 22, 2020

Let's consider an object called John.

const John = {
  name: "John Doe",
  age: 32,
  bio: "John Doe is a great guy",
};

We can get all the properties in this object using the Object.getOwnPropertyNames() method.

const John = {
  name: "John Doe",
  age: 32,
  bio: "John Doe is a great guy",
};

// get all property names
const arr = Object.getOwnPropertyNames(John);

/*

Result:
-------

["name", "age", "bio"]

*/
  • The Object.getOwnPropertyNames() method accepts the object to get all the property names as the parameter.
  • It returns an array containing the property names.

Feel free to share if you found this useful 😃.


Share on: Facebook Twitter