Instanceof operator in JavaScript


The Instanceof operator in JavaScript checks for the type of object at the run time environment. The result it returns is of Boolean type i.e., if the given input object is same as the object for which it is being checked, then it returns “true” or else it returns “false”.

Syntax

The syntax of Instanceof operator is shown below −

var myVar = nameOfObject instanceof typeOfObject

Instanceof operator is used in JavaScript for a unique purpose. As in JavaScript some of the variables are declared without mentioning the type of variable or the data type. In C, C++, Java the data types of the variables are to be mentioned. So in JavaScript to know the data type of the variable declared we use Instanceof operator so that the type of variable is object or any data type can be known.

Example 1

This example demonstrates the usage of Instanceof operator in JavaScript −

<!DOCTYPE html> <html> <body> <p id="arr1"></p> <p id="TP"></p> <script> var arr = ["Tutorials", "Point", "Articles"]; document.getElementById("arr1").innerHTML = "The given input is: " + arr + " type is: " + typeof(arr) document.getElementById("TP").innerHTML = "Check the instance of given object" + "<br>" + "For Array: " + (arr instanceof Array) + "<br>" + "For Number: " + (arr instanceof Number); </script> </body> </html>

Example 2

Following is another example of this operator −

<!DOCTYPE html> <html> <body> <p id="arr1"></p> <p id="TP"></p> <script> var departments = ["IT", "CSE", "ECE", "EEE"]; document.getElementById("arr1").innerHTML = "The given input is: " + departments + " and type of: " + typeof(departments) document.getElementById("TP").innerHTML = "Checking the instance of given object with different data types " + "<br>" + "for Array: " + (departments instanceof Array) + "<br>" + "for String: " + (departments instanceof String) + "<br>" + "for Object: " + (departments instanceof Object) + "<br>" + "for Number: " + (departments instanceof Number); </script> </body> </html>

Example 3

Using Strings and Date

Now, let us see the usage of the Instanceof operator with Sting and Date objects.

<!DOCTYPE html> <html> <body> <p id="ds"></p> <p id="TP"></p> <script> var Str = new String(); var Dt = new Date(); document.getElementById("ds").innerHTML = "The given input is: " + Dt + Str + " type of: " + typeof(Dt) + " " + typeof(Str) document.getElementById("TP").innerHTML = "Checking the instance of given objects with different data types: " + "<br>" + "with given Str for Object " + (Str instanceof Object) + "<br>" + "with given Str for Date: " + (Str instanceof Date) + "<br>" + "with given Str for String: " + (Str instanceof String) + "<br>" + "with given Dt for Date: " + (Dt instanceof Date) + "<br>" + "with given Dt for Object: " + (Dt instanceof Object) + "<br>" + "with given Dt for String: " + (Dt instanceof String); </script> </body> </html>

Example 4

With Primitive values

Strings and numbers are primitive values, not objects and therefore don't have a [[Prototype]], so it'll only work if you wrap them in regular objects.

console.log(1 instanceof Number) console.log(new Number(1) instanceof Number) console.log("" instanceof String) console.log(new String("") instanceof String)

Example 5

Constructible functions

Functions that return their objects(JS Classes) can have their objects checked using the instanceof operator.

function Person(name) { this.name = name } let john = new Person("John"); console.log(john instanceof Person)

Example 6

Inheritance

JS supports prototypical inheritance, so if you check for instanceof for any class in the hierarchy, it'll return true.

class Person {} class Student extends Person { constructor(name) { super() this.name = name } } let john = new Student("John"); console.log(john instanceof Person) console.log(john instanceof Student)

Updated on: 26-Aug-2022

763 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements