CKTIS / JavaScript
Object.getOwnPropertyNames — own property names
Object.getOwnPropertyNames(obj) returns a new array of string keys. For ordinary objects, these are the keys of all own properties with string keys, including non-enumerable ones. Symbol keys are excluded, and a Proxy's ownKeys trap can affect the list.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
Object.getOwnPropertyNames(obj)Object.getOwnPropertyNames(obj) is a static method: call it on Object, not on the inspected object. It returns a new array containing only the string keys from the result of the internal [[OwnPropertyKeys]] operation, without checking property descriptors. For ordinary objects, this includes every own string key regardless of the enumerable attribute, but no inherited property keys. The method returns keys, not property values.
For ordinary objects, Object.keys(obj) includes only enumerable own string-keyed properties. Object.getOwnPropertySymbols(obj) returns own Symbol keys, while Reflect.ownKeys(obj) returns both own string and Symbol keys, including keys of non-enumerable properties. “Own” and “enumerable” describe different characteristics.
For ordinary objects, names that are array indices appear first in ascending numeric order; other string names follow property creation order. This is not alphabetical sorting. Proxy objects can customize the list and order through the ownKeys trap, subject to specification constraints. If the target is extensible, the trap can report a string key with no corresponding own property descriptor or omit a configurable own string key of the target.
null and undefined cause a TypeError; other primitive values are converted to objects. The example demonstrates index ordering and inclusion of hidden, which defaults to non-enumerable with this use of Object.defineProperty. The inherited property and the Symbol key are omitted.
Examples
const obj = Object.create({ inherited: true });
obj[10] = 'ten';
obj[2] = 'two';
obj.label = 'demo';
Object.defineProperty(obj, 'hidden', { value: 42 });
obj[Symbol('token')] = 'secret';
console.log(JSON.stringify(Object.getOwnPropertyNames(obj)));
console.log(JSON.stringify(Object.keys(obj)));Result
["2","10","label","hidden"] ["2","10","label"]
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.