Object.getOwnPropertyNames()
Returns every own string-keyed property name, including the non-enumerable ones Object.keys skips.
Object.getOwnPropertyNames(obj)- Added in JavaScript ES5
Parameters
| Parameter | Type | Description |
|---|---|---|
$obj | Object | The object whose enumerable and non-enumerable own property names are to be returned. |
Return value
Array<string>An array of strings naming every own string-keyed property of the object, enumerable or not.
Errors
Throws a TypeError only if obj is null or undefined.
Changelog
- PHP ES2015A non-object argument is coerced to an object instead of throwing a TypeError, so Object.getOwnPropertyNames('ab') returns ['0', '1', 'length'].
Object.keys() shows only what a property is willing to advertise. Object.getOwnPropertyNames() shows everything with a string key that the object actually owns — enumerable or not — which makes it the tool for looking behind the curtain.
When to reach for it
- Enumerating the methods on a class: they are all non-enumerable, so
Object.keys(MyClass.prototype)returns an empty array while this returns['constructor', ...]. - Debugging an object where a key you can clearly read is absent from
Object.keys(). - Inspecting built-ins —
Object.getOwnPropertyNames(Math)lists the whole library.
What trips people up
It is own properties only, so it never walks the prototype chain: on a class instance you get the fields set in the constructor, not the methods. It also excludes symbol keys entirely — those need Object.getOwnPropertySymbols().
On arrays it returns the indices as strings plus 'length', so Object.getOwnPropertyNames([1, 2]) is ['0', '1', 'length']. Integer-like keys come first in ascending numeric order; the remaining string keys follow in insertion order.
Example
When to use it
- List every method a class defines, since Object.keys returns nothing for a prototype
- Find a property that is readable but invisible to Object.keys and JSON.stringify
- Enumerate the members of a built-in namespace object such as Math or JSON
Frequently asked questions
What is the difference between Object.keys and Object.getOwnPropertyNames?
Object.keys() returns only enumerable own string keys, while Object.getOwnPropertyNames() returns all own string keys including non-enumerable ones such as array length and class methods.
Why does Object.getOwnPropertyNames on a class instance not show its methods?
Methods are own properties of the prototype, not of the instance; call it on Class.prototype to see them.
How do I get symbol keys too?
Use Object.getOwnPropertySymbols() for symbols alone, or Reflect.ownKeys(obj) to get strings and symbols together in one array.
More examples
Listing the methods of a class
Every member declared in a class body is non-enumerable, so Object.keys() on the prototype is empty. getOwnPropertyNames() reveals the constructor, both methods and the size accessor. On the instance it lists only items, because methods are inherited rather than owned.
class Queue {
constructor() {
this.items = [];
}
push(v) {
this.items.push(v);
}
pop() {
return this.items.pop();
}
get size() {
return this.items.length;
}
}
console.log(Object.keys(Queue.prototype)); // []
console.log(Object.getOwnPropertyNames(Queue.prototype));
// [ 'constructor', 'push', 'pop', 'size' ]
console.log(Object.getOwnPropertyNames(new Queue())); // [ 'items' ]Walking the prototype chain for every key
Because getOwnPropertyNames() stops at own properties, reaching inherited members requires stepping up the chain with Object.getPrototypeOf() until it returns null. The result includes Object.prototype members such as hasOwnProperty and toString.
function allKeys(obj) {
const seen = new Set();
let current = obj;
while (current !== null) {
for (const key of Object.getOwnPropertyNames(current)) seen.add(key);
current = Object.getPrototypeOf(current);
}
return [...seen];
}
const keys = allKeys({ a: 1 });
console.log(keys.slice(0, 4)); // [ 'a', 'constructor', ... ]
console.log(keys.includes("hasOwnProperty")); // true
console.log(keys.includes("toString")); // true
Discussion