JavaScript Symbol hasInstance 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.
Below examples illustrate the JavaScript Symbol hasInstance Property:
Example 1:
javascript
let obj1 = [1, 2, 3];
let obj2 = [ 'a' , 'b' , 'c' ];
let obj3 = [123];
let obj4 = [];
console.log(Array[Symbol.hasInstance](obj1));
console.log(Array[Symbol.hasInstance](obj2));
console.log(Array[Symbol.hasInstance](obj3));
console.log(Array[Symbol.hasInstance](obj4));
|
Output:
true
true
true
true
Example-2:
javascript
function gfg() { }
let Script = new gfg
console.log(gfg[Symbol.hasInstance](Script));
|
Output:
true
Supported Browsers:
- Google Chrome 50 above
- Firefox 50 above
- Edge 15 above
- Opera 37 above
- Apple Safari 10 and above
We have a complete list of Javascript symbols’ properties and methods, to check those please go through the Javascript Symbol Complete Reference article.