Course contentsShow
← JavaScript
Lesson 630 of 2,72315. ObjectsFree lesson

Object.getOwnPropertyNames()

Retrieve all own property names, including non-enumerable ones, with Object.getOwnPropertyNames().

Object.getOwnPropertyNames()

What You'll Learn

You'll learn how to retrieve all own property names from an object, including those that don't show up in normal loops.

The Complete Property List

So far, you've used Object.keys() to get an object's property names, but it only returns enumerable properties. Sometimes you need to see everything an object owns, including properties marked as non-enumerable.

Object.getOwnPropertyNames() returns an array containing all own property names, whether they're enumerable or not.

Why This Matters

Think of enumerable properties as the "public directory" of an object—what most people see. Non-enumerable properties are like unlisted phone numbers: they exist and work, but they're intentionally hidden from casual browsing. Object.getOwnPropertyNames() gives you the complete directory.

The Syntax

const allProps = Object.getOwnPropertyNames(obj);

Example: Seeing Hidden Properties

const person = {
  name: "Alice",
  age: 30
};

// Add a non-enumerable property
Object.defineProperty(person, "id", {
  value: 12345,
  enumerable: false
});

console.log(Object.keys(person));
// ["name", "age"] - only enumerable

console.log(Object.getOwnPropertyNames(person));
// ["name", "age", "id"] - everything!

When to Use It

Use Object.getOwnPropertyNames() when you need to inspect or manipulate all properties on an object, especially when debugging or working with objects that have hidden properties. For everyday property iteration, Object.keys() is usually sufficient.

Key Takeaway: Object.getOwnPropertyNames() reveals all own properties on an object, including non-enumerable ones that Object.keys() skips—perfect for complete object inspection.