The JavaScript Reflect.has() method determines if a property exists in an object. It is similar to in operator as a function.
Syntax:
Reflect.has(target, propertyKey)
Parameters:
target: It represents the object on which property existing check have to be perform.
propertyKey: It represents the name of the property to check.
Return:
It returns true if the specified property is exist in the object otherwise returns false.
Note: It throws TypeError, if target is not an Object.
Example 1:
<!DOCTYPE html> <html> <body> <script> const obj = {prop:100}; document.write(Reflect.has(obj, 'prop')); </script> </body> </html> |
Output:
true |
Example 2:
<!DOCTYPE html> <html> <body> <script> const object1 = { property1: "w3spoint" }; document.write(Reflect.has(object1, 'property1')); document.write("</br>"); document.write(Reflect.has(object1, 'property2')); document.write("</br>"); document.write(Reflect.has(object1, 'toString')); </script> </body> </html> |
Output:
true false true |