JavaScript | Reflect.has() Method

The Reflect.has() method in JavaScript is used to check whether the property exists in an object or not. It works like in operator as a function.

Syntax:

Reflect.has(target, propertyKey) 

Parameters: This method accept two parameters as mentioned above and described below:



  • target: This parameter is the target object and it looks for the property.
  • propertyKey: This parameter is the name of the property to be checked.

Return value: This methods returns a Boolean value which indicates if the target has the property.

Exceptions: A TypeError is exception given as the result, when the target is not object.

Below examples illustrate the Reflect.has() method in JavaScript:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
const object1 = {
    property1: 434
};
  
console.log(Reflect.has(object1, 'property1'));
console.log(Reflect.has(object1, 'property2'));
console.log(Reflect.has(object1, 'toString'));
  
var x = { foo: 1 }; 
console.log(Reflect.has(x, 'foo')); 
console.log('foo' in x); 
console.log(Reflect.has(x, 'bar')); 
console.log('bar' in x); 
</script>

chevron_right


Output:

true
false
true
true
true
false
false

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
// Returns true for properties in
// the prototype chain 
console.log(Reflect.has({x: 0}, 'toString') );
  
// Proxy with .has() handler method
let obj = new Proxy({}, {
    has(t, k) { return k.startsWith('geeks')  }
});
console.log(Reflect.has(obj, 'geeksforgeeks'));
console.log(Reflect.has(obj, 'geekforgeek'));
  
const val1 = {foo: 123}
const val2 = {__proto__: val1}
const val3 = {__proto__: val2}
  
// The prototype chain is: c -> b -> a
console.log(Reflect.has(val3, 'foo'));
</script>

chevron_right


Output:

true
true
false
true

Supported Browsers: The browsers supported by JavaScript Reflect.has() Method are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari
  • Edge

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.