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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

> true
> true
> true
> true

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

> true

Supported Browsers:

  • Google Chrome 51
  • Firefox 50
  • Edge 15
  • Opera
  • Apple Safari

Reference: https://devdocs.io/javascript/global_objects/symbol/hasinstance



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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.