JavaScript | Symbol.hasInstance Property
The Symbol.hasInstance is an inbuilt property in JavaScript which is used to determine if a given constructor object recognizes the object as its instance.
Syntax:
[Symbol.hasInstance](Object)
Parameters: It accepts a parameter “object”.
Return value: This returns true if the value is in the chain of the object otherwise false.
JavaScript code to show the working of this function.
Example-1:
javascript
<script> // Initialising some objects var obj1 = [1, 2, 3]; var obj2 = [ 'a' , 'b' , 'c' ]; var obj3 = [123]; var obj4 = []; // Calling Symbol.hasInstance Property console.log( Array[Symbol.hasInstance](obj1)); console.log( Array[Symbol.hasInstance](obj2)); console.log( Array[Symbol.hasInstance](obj3)); console.log( Array[Symbol.hasInstance](obj4)); </script> |
Output:
> true > true > true > true
Example-2:
javascript
<script> // Calling a user define function function gfg() {} // Initialising the object var Script = new gfg // Calling the Symbol.hasInstance property console.log(gfg[Symbol.hasInstance](Script)); </script> |
Output:
> true
Supported Browsers:
- Google Chrome 50 above
- Firefox 50 above
- Edge 15 above
- Opera 37 above
- Apple Safari 10 and above
Reference: https://devdocs.io/javascript/global_objects/symbol/hasinstance