Symbol.hasInstance JavaScript

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

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[Symbol.hasInstance] (object)
[Symbol.hasInstance] (object)
[Symbol.hasInstance] (object)

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

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

Example 1:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script>
var alpha = ['a','b'];
document.write( Array[ Symbol.hasInstance ](alpha) );
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script> var alpha = ['a','b']; document.write( Array[ Symbol.hasInstance ](alpha) ); </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<script>
var alpha = ['a','b'];
document.write( Array[ Symbol.hasInstance ](alpha) );
</script>
</body>
</html>

Example 2:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script>
class ArrayTest {
static [Symbol.hasInstance](instance) {
return Array.isArray(instance);
}
}
document.write([] instanceof ArrayTest);
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script> class ArrayTest { static [Symbol.hasInstance](instance) { return Array.isArray(instance); } } document.write([] instanceof ArrayTest); </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<script>
class ArrayTest {
static [Symbol.hasInstance](instance) {
return Array.isArray(instance);
}
}
document.write([] instanceof ArrayTest);
</script>
</body>
</html>