The static method Reflect.has serves the purpose of determining whether a specific property is present within an object. It functions similarly to the in operator but operates as a callable function.
Syntax:
Example
Reflect.has(target, propertyKey)
Parameters:
target: It refers to the entity in which the desired property is sought.
propertyKey: This refers to the identifier of the property that is subject to verification.
Return value:
It provides a Boolean value that signifies whether the target possesses the specified property.
Exceptions:
A TypeError, if the target is not an Object.
Browser Support:
| Chrome | 49 |
|---|---|
Edge |
12 |
| Firefox | 42 |
| Opera | 36 |
Example 1
Example
const object1 = {
property1: 42
};
console.log(Reflect.has(object1, 'property1'));
Output:
Example 2
Example
const object1 = {
property1: 42
};
console.log(Reflect.has(object1, 'property2'));
Output:
Example 3
Example
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);
Output:
Output
true
true
false
false