JavaScript Reference ยท Object
Object.getOwnPropertyNames()
Reference for JavaScript Object.getOwnPropertyNames(): list all own string-keyed properties, including non-enumerable ones, and how it differs from Object.keys().
Object.getOwnPropertyNames() returns an array of the names of all properties that an object owns directly, including non-enumerable ones. Only string keys are included; symbols are not.
It is the "show me everything" counterpart of Object.keys(), which lists only enumerable properties. It is often used to explore built-in objects and class prototypes, whose methods are non-enumerable.
Object.getOwnPropertyNames([1, 2]); // ["0", "1", "length"]
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Object.getOwnPropertyNames(obj)
| Name | Description |
|---|---|
obj |
The object whose own property names are returned. Primitives are converted to objects. |
Return value: a new array of strings.
Exceptions: a TypeError if obj is null or undefined.
Usage notes
- Includes hidden members. Array
length, class methods, and properties defined withenumerable: falseall appear.Object.keys()omits them. - No symbols. Use
Object.getOwnPropertySymbols(), orReflect.ownKeys()for strings and symbols together. - Order: integer-like keys in ascending order first, then other strings in creation order.
- Not inherited. Methods from the prototype chain are not listed, so to see them walk up with
Object.getPrototypeOf(). - Classes: methods and accessors are non-enumerable, so they show up here (with
constructor) but not inObject.keys(). Static members appear on the class constructor. - Strings: a string yields index names plus
length. - Private fields (
#name) are never listed. - Mistake to avoid: do not use it to serialize data. Hidden properties are usually hidden on purpose.
Browser support
Object.getOwnPropertyNames() is available in every browser and JavaScript runtime.