Symbol.hasInstance JavaScript

The JavaScript Symbol.hasInstance property finds out that a constructor object recognizes an object as its instance or not.

Syntax:

[Symbol.hasInstance] (object)

Parameters:
object: It represents the specific object to be checked as symbol instance.

Returns:
It return true if the object is an instance of symbol, otherwise returns false.

Example 1:

<!DOCTYPE html>
<html>
<body>
<script>   
var alpha = ['a','b'];    
document.write( Array[ Symbol.hasInstance ](alpha) );   
</script>
</body>
</html>

Output:

true

Example 2:

<!DOCTYPE html>
<html>
<body>
<script>  
class ArrayTest {
  static [Symbol.hasInstance](instance) {
    return Array.isArray(instance);
  }
}
 
document.write([] instanceof ArrayTest);
 
</script>
</body>
</html>

Output:

true
Please Share