What is Reflect.ownKeys() in JavaScript?

The Reflect.ownKeys() method returns the object’s own property name and all symbol properties of the object as an array.

Syntax

Reflect.ownKeys(targetObj);

Parameter:

  • targetObj: the object to get the keys of.

The argument targetObj must be an object. Otherwise, a TypeError will be thrown.

The returned array doesn’t contain the property keys that are present in their prototype chain.

Example

let obj = {
name : "Educative",
field : "Education",
rating : 4,
[Symbol('a')] : "Symbol A",
[Symbol('b')] : "Symbol B"
};
let keys = Reflect.ownKeys(obj);
console.log(keys);

In the code above, we created an object with some properties and symbols. Then we called the Reflect.ownKeys method, which will return all the object’s own property names and symbol properties of the object as an array.

Free Resources