Object.getOwnPropertyNames()

ES5+

Returns an array of all properties found directly in a given object.

Syntax

Object.getOwnPropertyNames(obj)

Parameters

obj Object

The object whose enumerable and non-enumerable properties are to be returned

Return Value

Array

An array of strings corresponding to the properties

Examples

JavaScript
const obj = { a: 1, b: 2 };
Object.defineProperty(obj, 'c', { value: 3, enumerable: false });
console.log(Object.keys(obj));
console.log(Object.getOwnPropertyNames(obj));
Output:
// ['a', 'b'] ['a', 'b', 'c']

📌 When to Use

Use Object.getOwnPropertyNames() when you need ALL property names including non-enumerable ones, implementing complete object inspection tools, finding hidden properties for debugging, or when you need a more complete picture of an object than Object.keys() provides.

⚠️ Common Mistakes

Using Object.getOwnPropertyNames() when Object.keys() would suffice - the extra non-enumerable properties are rarely needed.

Forgetting that Symbol properties are not included - use Object.getOwnPropertySymbols() for those.

Expecting inherited properties - this method only returns own properties, not those from the prototype chain.

✅ Best Practices

For complete property enumeration including Symbols, combine with getOwnPropertySymbols: [...Object.getOwnPropertyNames(obj), ...Object.getOwnPropertySymbols(obj)]

Use Reflect.ownKeys(obj) as a modern alternative that returns both string and Symbol property names.

Create debugging tools that compare Object.keys() vs Object.getOwnPropertyNames() to find hidden properties.

⚡ Performance Notes

Object.getOwnPropertyNames() is slightly slower than Object.keys() because it must also check the enumerable flag for each property. However, both are O(n) operations. In practice, the difference is negligible. For bulk property operations, prefer using the method that returns exactly what you need.

🌍 Real World Example

Complete Object Property Inspector

Build a comprehensive object inspector that reveals all properties including hidden (non-enumerable) ones for debugging.

function inspectObject(obj) {
  const enumerable = Object.keys(obj);
  const allProps = Object.getOwnPropertyNames(obj);
  const symbols = Object.getOwnPropertySymbols(obj);
  const nonEnumerable = allProps.filter(p => !enumerable.includes(p));

  return {
    enumerable: enumerable,
    nonEnumerable: nonEnumerable,
    symbols: symbols.map(s => s.toString()),
    prototype: Object.getPrototypeOf(obj)?.constructor?.name || null,
    summary: {
      total: allProps.length + symbols.length,
      visible: enumerable.length,
      hidden: nonEnumerable.length,
      symbolCount: symbols.length
    }
  };
}

// Create an object with various property types
const myObj = { visible1: 'a', visible2: 'b' };

// Add non-enumerable property
Object.defineProperty(myObj, 'hidden', {
  value: 'secret',
  enumerable: false
});

// Add Symbol property
const mySymbol = Symbol('mySymbol');
myObj[mySymbol] = 'symbol value';

// Inspect the object
const inspection = inspectObject(myObj);
console.log(inspection);
// {
//   enumerable: ['visible1', 'visible2'],
//   nonEnumerable: ['hidden'],
//   symbols: ['Symbol(mySymbol)'],
//   prototype: 'Object',
//   summary: { total: 4, visible: 2, hidden: 1, symbolCount: 1 }
// }

// Compare different methods
console.log('Object.keys():', Object.keys(myObj));
// ['visible1', 'visible2']

console.log('Object.getOwnPropertyNames():', Object.getOwnPropertyNames(myObj));
// ['visible1', 'visible2', 'hidden']

console.log('Reflect.ownKeys():', Reflect.ownKeys(myObj));
// ['visible1', 'visible2', 'hidden', Symbol(mySymbol)]

Related Methods