instanceof

The Left Hand Side (LHS) operand is the actual object being tested to the Right Hand Side (RHS) operand which is the actual constructor of a class. The basic definition is:

Checks the current object and returns true if the object is of the specified object type.

Here are some good examples and here is an example taken directly from Mozilla's developer site:

var color1 = new String("green");
color1 instanceof String; // returns true
var color2 = "coral"; //no type specified
color2 instanceof String; // returns false (color2 is not a String object)

One thing worth mentioning is instanceof evaluates to true if the object inherits from the class's prototype:

var p = new Person("Jon");
p instanceof Person

That is p instanceof Person is true since p inherits from Person.prototype.

Per the OP's request

I've added a small example with some sample code and an explanation.

When you declare a variable you give it a specific type.

For instance:

int i;
float f;
Customer c;

The above show you some variables, namely i, f, and c. The types are integer, float and a user defined Customer data type. Types such as the above could be for any language, not just JavaScript. However, with JavaScript when you declare a variable you don't explicitly define a type, var x, x could be a number / string / a user defined data type. So what instanceof does is it checks the object to see if it is of the type specified so from above taking the Customer object we could do:

var c = new Customer();
c instanceof Customer; //Returns true as c is just a customer
c instanceof String; //Returns false as c is not a string, it's a customer silly!

Above we've seen that c was declared with the type Customer. We've new'd it and checked whether it is of type Customer or not. Sure is, it returns true. Then still using the Customer object we check if it is a String. Nope, definitely not a String we newed a Customer object not a String object. In this case, it returns false.

It really is that simple!

Answer from JonH on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › instanceof
instanceof - JavaScript - MDN Web Docs
July 8, 2025 - Because all functions inherit from Function.prototype by default, most of the time, the Function.prototype[Symbol.hasInstance]() method specifies the behavior of instanceof when the right-hand side is a function. See the Symbol.hasInstance page for the exact algorithm of instanceof. JavaScript execution environments (windows, frames, etc.) are each in their own realm.
🌐
W3Schools
w3schools.com › jsref › jsref_oper_instanceof.asp
JavaScript instanceof Operator
: ?. ... () ? x : y => delete in instanceof typeof void JS Precedence JS Promises
Top answer
1 of 11
318

instanceof

The Left Hand Side (LHS) operand is the actual object being tested to the Right Hand Side (RHS) operand which is the actual constructor of a class. The basic definition is:

Checks the current object and returns true if the object is of the specified object type.

Here are some good examples and here is an example taken directly from Mozilla's developer site:

var color1 = new String("green");
color1 instanceof String; // returns true
var color2 = "coral"; //no type specified
color2 instanceof String; // returns false (color2 is not a String object)

One thing worth mentioning is instanceof evaluates to true if the object inherits from the class's prototype:

var p = new Person("Jon");
p instanceof Person

That is p instanceof Person is true since p inherits from Person.prototype.

Per the OP's request

I've added a small example with some sample code and an explanation.

When you declare a variable you give it a specific type.

For instance:

int i;
float f;
Customer c;

The above show you some variables, namely i, f, and c. The types are integer, float and a user defined Customer data type. Types such as the above could be for any language, not just JavaScript. However, with JavaScript when you declare a variable you don't explicitly define a type, var x, x could be a number / string / a user defined data type. So what instanceof does is it checks the object to see if it is of the type specified so from above taking the Customer object we could do:

var c = new Customer();
c instanceof Customer; //Returns true as c is just a customer
c instanceof String; //Returns false as c is not a string, it's a customer silly!

Above we've seen that c was declared with the type Customer. We've new'd it and checked whether it is of type Customer or not. Sure is, it returns true. Then still using the Customer object we check if it is a String. Nope, definitely not a String we newed a Customer object not a String object. In this case, it returns false.

It really is that simple!

2 of 11
97

There's an important facet to instanceof that does not seem to be covered in any of the comments thus far: inheritance. A variable being evaluated by use of instanceof could return true for multiple "types" due to prototypal inheritance.

For example, let's define a type and a subtype:

function Foo(){ //a Foo constructor
    //assign some props
    return this;
}

function SubFoo(){ //a SubFoo constructor
    Foo.call( this ); //inherit static props
    //assign some new props
    return this;
}

SubFoo.prototype = Object.create(Foo.prototype); // Inherit prototype
SubFoo.prototype.constructor = SubFoo;

Now that we have a couple of "classes" lets make some instances, and find out what they're instances of:

var 
    foo = new Foo()
,   subfoo = new SubFoo()
;

alert( 
    "Q: Is foo an instance of Foo? "
+   "A: " + ( foo instanceof Foo ) 
); // -> true

alert( 
    "Q: Is foo an instance of SubFoo? " 
+   "A: " + ( foo instanceof SubFoo ) 
); // -> false

alert( 
    "Q: Is subfoo an instance of Foo? "
+   "A: " + ( subfoo instanceof Foo ) 
); // -> true

alert( 
    "Q: Is subfoo an instance of SubFoo? "
+   "A: " + ( subfoo instanceof SubFoo ) 
); // -> true

alert( 
    "Q: Is subfoo an instance of Object? "
+   "A: " + ( subfoo instanceof Object ) 
); // -> true

See that last line? All "new" calls to a function return an object that inherits from Object. This holds true even when using object creation shorthand:

alert( 
    "Q: Is {} an instance of Object? "
+   "A: " + ( {} instanceof Object ) 
); // -> true

And what about the "class" definitions themselves? What are they instances of?

alert( 
    "Q: Is Foo an instance of Object? "
+   "A:" + ( Foo instanceof Object) 
); // -> true

alert( 
    "Q: Is Foo an instance of Function? "
+   "A:" + ( Foo instanceof Function) 
); // -> true

I feel that understanding that any object can be an instance of MULTIPLE types is important, since you may (incorrectly) assume that you could differentiate between, say an object and a function by use of instanceof. As this last example clearly shows a function is an object.

This is also important if you are using any inheritance patterns and want to confirm the progeny of an object by methods other than duck-typing.

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › instanceof-operator-in-javascript
JavaScript Instanceof Operator - GeeksforGeeks
July 11, 2025 - <!DOCTYPE html> <html lang="en"> <head> <title> How to Check/Uncheck the checkbox using JavaScript ? </title> </head> <body> <h1 style="color:green"> GeeksforGeeks </h1> <h3> Instanceof Operator. </h3> <p id="GFG"></p> <script> let a = ["Geeks", "for", "Geeks"]; document.getElementById("GFG").innerHTML = (a instanceof Array) + "<br>" + (a instanceof Number); </script> </body> </html> ... Example 2: Below is the example of the Instanceof Operator.
🌐
Flexiple
flexiple.com › javascript › instanceof-javascript
JavaScript instanceof operator: Syntax, Example & Explanation - Flexiple
March 11, 2022 - var = ["Apple", "Mango", "Banana"]; console.log(fruits instanceof Array); console.log(fruits instanceof Object); console.log(fruits instanceof Number); console.log(fruits instanceof String); //Output true true false false
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › classes
Class checking: "instanceof"
September 26, 2025 - It also takes inheritance into account. Such a check may be necessary in many cases. For example, it can be used for building a polymorphic function, the one that treats arguments differently depending on ...
🌐
DEV Community
dev.to › codecupdev › learn-how-to-use-instanceof-in-javascript-3b0i
Learn how to use instanceof in JavaScript - DEV Community
October 18, 2022 - In the above example we create an object literal stored in a variable called pets. Inside of the pets object we create a property called amount with a value of four. Next we use the instanceof operator to check whether pets is an instance of ...
🌐
CoreUI
coreui.io › blog › what-is-the-difference-between-typeof-and-instanceof-in-javascript
What is the difference between typeof and instanceof in JavaScript · CoreUI
September 17, 2024 - Use instanceof when dealing with objects, especially to determine inheritance and prototype relationships. By carefully choosing the appropriate operator and being aware of their quirks and limitations, you can write more robust, maintainable, and error-free code. Remember: Always test your type checks and be mindful of JavaScript’s special cases to avoid unexpected behavior.
Find elsewhere
🌐
W3Resource
w3resource.com › javascript › operators › instanceof.php
JavaScript : instanceof Operator - w3resource
August 19, 2022 - In JavaScript instanceof operator is used to check the type of an object at run time. The instanceof operator returns a boolean value that indicates if an object is an instance of a particular class.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
How does the "instanceof" operator work? - JavaScript - The freeCodeCamp Forum
February 5, 2023 - Tell us what’s happening: How does theinstanceof operator infer that a parent object, is a child object’s constructor, without explicitly setting the constructor property inside parent.prototype? I understand that by overwriting Dog.prototype with a new object (to improve inheritance ...
🌐
TutorialsPoint
tutorialspoint.com › instanceof-operator-in-javascript
Instanceof operator in JavaScript
August 26, 2022 - <!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>
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript instanceof
JavaScript instanceof
October 6, 2023 - class Person { constructor(name) { this.name = name; } } class Employee extends Person { constructor(name, title) { super(name); this.title = title; } } let e1 = new Employee(); console.log(e1 instanceof Employee); // true console.log(e1 instanceof ...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › javascript tutorial › javascript instanceof
JavaScript instanceof | Guide to JavaScript instanceof with SampleCode
July 6, 2023 - JavaScript instanceof operator returns 2 values true or Whenever an instance of an object comparison type both are same then returns true otherwise return false.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
sebhastian
sebhastian.com › instanceof-javascript
The JavaScript instanceof operator explained - with syntax and examples | sebhastian
March 24, 2022 - As you can see, the dog variable is an instance of Dog, so the instanceof operator returns true. JavaScript also has the typeof operator that can be used to check the type of values you define in your code.
🌐
Medium
medium.com › @AlexanderObregon › the-mechanics-behind-javascripts-typeof-and-instanceof-3299ff38f20a
The Mechanics of JavaScript’s typeof and instanceof | Medium
May 16, 2025 - The item still points to the old prototype, so instanceof returns false. Again, the actual constructor is irrelevant, it’s the current state of the prototype link that decides the result. JavaScript even lets you customize how instanceof works using Symbol.hasInstance.
🌐
Learn TypeScript
learntypescript.dev › 07 › l4-instanceof-type-guard
Using an `instanceof` type guard | Learn TypeScript
In this lesson, we will learn about the instanceof operator and how it can be used to narrow the type of class objects.
🌐
John Kavanagh
johnkavanagh.co.uk › home › articles › instanceof: understanding javascript object types
instanceof: Understanding JavaScript Object Types, by John Kavanagh
June 1, 2024 - Uncover the differences between JavaScript's instanceof and typeof for type checking, with practical examples and insights on when to use each operator.
Price   $$
Address   66 Paul Street, EC2A 4NE, London
🌐
W3Schools
w3schools.com › js › tryit.asp
JavaScript Operators
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
W3cubDocs
docs.w3cub.com › javascript › operators › instanceof
Instanceof - JavaScript - W3cubDocs
The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return …