Skip to content
Related Articles

Related Articles

JavaScript | Symbol.hasInstance Property
  • Last Updated : 06 May, 2019

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:






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




<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 51
  • Firefox 50
  • Edge 15
  • Opera
  • Apple Safari

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

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :