Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript | Reflect.ownKeys() Method

  • Last Updated : 30 Sep, 2021

The Reflect.ownKeys() method in Javascript is used to return an array of the target object’s own property keys and it ignores the inherited properties.
Syntax: 
 

Reflect.ownKeys( obj )

Parameters: This method accepts a single parameter as mentioned above and described below: 
 

  • Obj: This parameter holds the target object and it is used to get the own keys.

Return value: This method always returns the Array of the target object’s own property keys.
Exceptions: A TypeError is exception given as the result, when the target is not an Object.
Below examples illustrate the Reflect.ownKeys() method in JavaScript:
Example 1: 
 

javascript




const object1 = {
  property1: 332,
  property2: 2
};
 
const array1 = [];
 
console.log(Reflect.ownKeys(object1));
 
console.log(Reflect.ownKeys(array1));
 
const obj = {ab: 5, bc: 5}; 
const obj1 = {ab: 5, bc: 5, ca:7}; 
   
console.log(Reflect.ownKeys(obj));   
console.log(Object.keys(obj1)); 
console.log(Reflect.ownKeys(obj1));

Output: 
 

[ 'property1', 'property2' ]
[ 'length' ]
[ 'ab', 'bc' ]
[ 'ab', 'bc', 'ca' ]
[ 'ab', 'bc', 'ca' ]

Example 2: 
 

javascript




console.log(Reflect.ownKeys({z: 3, y: 2, x: 1}));
console.log(Reflect.ownKeys([]));                
  
let sym = Symbol.for('comet')
let sym2 = Symbol.for('meteor')
let obj = {[sym]: 0, 'val': 0, '45': 0, 'sdf': 0,
           [sym2]: 0, 'safss': 0, '34': 0, 'val2': 0}
console.log(Reflect.ownKeys(obj));
  
var obj1 = Object.create({}, { hoo:
    { value: function() { return this.hoo; } } }); 
console.log(Object.keys(obj1));  
console.log(Reflect.ownKeys(obj1));

Output: 
 

[ 'z', 'y', 'x' ]
[ 'length' ]
[
  '34',
  '45',
  'val',
  'sdf',
  'safss',
  'val2',
  Symbol(comet),
  Symbol(meteor)
]
[]
[ 'hoo' ]

Supported Browsers: The browsers supported by JavaScript Reflect.ownKeys() method are listed below: 
 

  • Google Chrome 49 and above
  • Edge 12 and above
  • Firefox 42 and above
  • Opera 36 and above
  • Safari 10 and above

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!