Reflect.ownKeys(target) returns an array of all the own keys of an object. It includes string keys and symbol keys, and it includes properties that are hidden from loops (non-enumerable).
That makes it more complete than Object.keys(), which only lists enumerable string keys. Inherited properties are never included. The keys come in a fixed order: whole numbers first, then strings in the order they were added, then symbols.
Remember
Reflect.ownKeys(target)
returns an array with:
1) integer keys, smallest first
2) string keys, in order of creation
3) symbol keys, in order of creation
includes hidden (non-enumerable) keys
target must be an object, or TypeError
// ECMAScript 2015 (ES6)
Reflect.ownKeys(target)
// target : an object
// returns: an array of strings and symbols
// throws : TypeError if target is not an object
Parameters
Parameter
Description
target
Required. The object whose keys you want. If it is not an object, a TypeError is thrown.
Return Value
Return Value
Description
Array
All own keys. Numbers are returned as strings, such as "1". Symbols are returned as symbols.
TypeError (thrown)
target is not an object.
JavaScript Examples
Example 1 — List the Keys and Their Order
See the order of the keys: numbers first, then names in the order they were added.
Object.keys: name
Reflect.ownKeys: name, secret, Symbol(tag)
Symbol keys: 1
Hidden but readable: 42
How It Works
Object.keys is short. It lists only enumerable string keys, so it finds just name.
ownKeys is complete. It also finds secret (hidden, because defineProperty() made it non-enumerable) and the symbol, which comes last. map(String) turns the symbol into readable text.
Filter by type.typeof key === "symbol" picks out the symbol keys. The hidden property can still be read directly.
Example 3 — Own Keys Only and Errors
Inherited keys and class methods are not listed. A primitive value throws. The catch block prints the error type only.
Instance data only. The instance owns name. The method speak lives on the prototype, so it is not listed for the instance.
The prototype has its own keys. Asking for the keys of Pet.prototype shows constructor and speak.
Inherited keys are skipped. The child has no own keys, so the list is empty. A string is not an object, so the last call throws a TypeError.
Compatibility
Browser & Runtime Support
Reflect.ownKeys() is part of ES2015 (ES6), together with the Reflect object. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
Reflect.ownKeys()
Supported in Chrome 49+, Edge 12+, Firefox 42+, Safari 10+, Opera 36+ and Node.js 6+. Internet Explorer never got it.
ES2015Standard since
Google Chrome49+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium and legacy
Full support
Mozilla Firefox42+ · Desktop & Mobile
Full support
Apple Safari10+ · macOS & iOS
Full support
Opera36+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView49+ · Chromium based
Supported
Node.js6+ · Server runtime
Supported
DenoSupported · JavaScript runtime
Supported
BunSupported · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
Reflect.ownKeys()Excellent
Bottom line: Safe to use in every current browser and in Node.js. The only thing to plan for is Internet Explorer, which has no Reflect at all.
Frequently Asked Questions
It returns an array with every own key of an object: string keys, number keys (as strings) and symbol keys, including hidden (non-enumerable) properties. Inherited keys are not included.
Object.keys() lists only enumerable string keys. Reflect.ownKeys() lists all own keys, including symbols and non-enumerable properties such as the length of an array.
First whole-number keys from smallest to largest, then string keys in the order they were created, then symbol keys in the order they were created.
If the object has a symbol key, the array contains a symbol, and a symbol cannot be turned into text automatically. Use keys.map(String).join(", ") or filter the symbols out first.
A Proxy can change the list of keys with an ownKeys trap. Inside the trap, Reflect.ownKeys(target) gives the normal list, which you can then filter, for example to hide keys that start with an underscore.