What is an instanceof operator in JavaScript?

In JavaScript, the operator namely instanceof is helpful to check the object’s type at run time. It returns true if the object is an instance of a specific constructor otherwise the return value will be false.

instanceof Operator in JavaScript
instanceof Operator in JavaScript


objectName instanceof constructor


objectName: it shows the object name.

Return value

It returns the Boolean value i.e. true or false. If the object is the instance of a specific constructor true will be returned otherwise false.


Let’s discuss an example to testify the type of object by using the instanceof operator in JS.

<!DOCTYPE html>
      <h2 style="color:red">JS Example Code</h2>
      <p id="test"></p>
         var subjects= ["Maths", "Biology", "Physics"];
         document.getElementById("test").innerHTML =
         (subjects instanceof Object) + "<br>" + 
         (subjects instanceof Number) + "<br>" +
         (subjects instanceof Array) + "<br>" +
         (subjects instanceof String);
  • Line#7: Here we have declared and initialized an array namely "subjects".
  • Line#9: This part of the code checks whether the defined array is an instance of a class Object type or not.
  • Line#10: This (subjects instanceof Number) code checks whether the defined array is an instance of class Number.
  • Line#11: (subjects instanceof Array) checks whether the defined subjects is an instance of class Array.
  • Line#12: This part of the code checks whether the defined array is an instance of a class String.


Program output for instanceof operator

Importance of instanceof operation in JavaScript

Variable declaration in JavaScript does not define the type of the variable. For example, we declare the variable like var x. This variable x can be a number, array, string, or user-defined data type.

In various other languages like C++, we declare the variable in a way like an int a or double b, etc. we can check that the object is related to the specific type or not by using instanceof operation in JavaScript. Thus, it is a useful operator of JavaScript.

Stay in the Loop

Get the daily email from Algoideas that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...