inspection

Object.getOwnPropertyNames

Returns an array of all own string-keyed property names, including non-enumerable ones (but not Symbol keys). Useful for introspection where Object.keys is too restrictive.

Object.getOwnPropertyNames(obj)

Parameters

Parameter Purpose
obj The object whose own string-keyed properties are to be returned
returns Array of all own string property names

Examples

const o = Object.defineProperty({}, 'hidden', { value: 1, enumerable: false });
o.visible = 2;
console.log(Object.getOwnPropertyNames(o));

Logs [ 'hidden', 'visible' ]

console.log(Object.getOwnPropertyNames([1, 2, 3]));

Logs [ '0', '1', '2', 'length' ] — length is non-enumerable

console.log(Object.keys({ a: 1 }));
console.log(Object.getOwnPropertyNames({ a: 1 }));

Both log [ 'a' ] when all keys are enumerable

const s = Symbol('id');
console.log(Object.getOwnPropertyNames({ [s]: 1, name: 'x' }));

Logs [ 'name' ] — Symbols excluded; use getOwnPropertySymbols for those

Gotcha

Includes non-enumerable string keys but STILL excludes Symbol keys. Use Reflect.ownKeys(obj) to get strings + Symbols + non-enumerables in one call.

Related methods

← All JS Object methods · Array methods · Spread vs rest