🌐
WebPlatform
webplatform.github.io › docs › javascript › operators › instanceof
instanceof · WebPlatform Docs
Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github. DOCS · javascript · operators · instanceof · Returns a Boolean value that indicates whether or not an object is an instance of a particular class.
🌐
GitHub
github.com › mdn › content › blob › main › files › en-us › web › javascript › reference › operators › instanceof › index.md
content/files/en-us/web/javascript/reference/operators/instanceof/index.md at main · mdn/content
For [bound functions](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind), `instanceof` looks up for the `prototype` property on the target function, since bound functions don't have `prototype`.
Author   mdn
🌐
GitHub
github.com › KROT47 › instanceof
GitHub - KROT47/instanceof: JavaScript all types detection module (crossbrowser)
JavaScript all types detection module (crossbrowser) - KROT47/instanceof
Author   KROT47
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › instanceof
instanceof - JavaScript - MDN Web Docs
July 8, 2025 - class C { #value = "foo"; static [Symbol.hasInstance](x) { return this === C && #value in x; } } class D extends C {} console.log(new C() instanceof D); // false console.log(new C() instanceof C); // true console.log({ __proto__: C.prototype } instanceof C); // false ... This page was last modified on Jul 8, 2025 by MDN contributors. View this page on GitHub • Report a problem with this content
🌐
Mostafa-samir
mostafa-samir.github.io › classingjs › tutorial › instanceof.html
Tutorial : Object::instanceOf
var Identifiable = classing.Interface({ getIdentified: function(){} }); var IPayable = classing.Interface({ getPaid : function(){} }); var Person = classing.Class.Implements(Identifiable)({ private : { id : null, }, protected : { name : null, age : null, }, public : { Construct : Function.create(xTyped , [ types(), function() { this.id = Date.now(); }, types(String , Number), function(str , num) { this.name = str; this.age = num; this.id = Date.now(); } ]), getIdentified : function() { return this.id; } } }); var Employee = classing.Class.Extends(Person).Implements(IPayable)({ protected : { sa
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › classes
Class checking: "instanceof"
September 26, 2025 - So, by the logic of instanceof, the prototype actually defines the type, not the constructor function. ... If you have suggestions what to improve - please submit a GitHub issue or a pull request instead of commenting.
🌐
GitHub
github.com › mdn › translated-content-de › blob › main › files › de › web › javascript › reference › operators › instanceof › index.md
translated-content-de/files/de/web/javascript/reference/operators/instanceof/index.md at main · mdn/translated-content-de
January 21, 2025 - Für [gebundene Funktionen](/de/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) sucht `instanceof` nach der `prototype`-Eigenschaft auf der Ziel-Funktion, da gebundene Funktionen kein `prototype` haben.
Author   mdn
Find elsewhere
🌐
GitHub
github.com › topics › instanceof
instanceof · GitHub Topics · GitHub
February 8, 2017 - The instanceof mechanism cross package/module versions. ... Test if an object's prototype chain contains a provided prototype. nodejs javascript utility node prototype utilities utils stdlib oop class inheritance util object-oriented assert node-js assertion proto instanceof inherits inherit
🌐
Flexiple
flexiple.com › javascript › instanceof-javascript
JavaScript instanceof operator: Syntax, Example & Explanation - Flexiple
March 11, 2022 - i.e. we just use var xyz; which could be a string, number, array, or a user-defined datatype as opposed to other languages, for example, in C or C+ we specify the datatype while declaring a variable i.e. int i; float f, etc. Hence, having instanceof operator to check if an object belongs to a certain specified type would be useful in JavaScript.
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.

🌐
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. Follow Łukasz Holeczek on GitHub Connect with Łukasz Holeczek on LinkedIn Follow Łukasz Holeczek on X (Twitter)
🌐
GeeksforGeeks
geeksforgeeks.org › instanceof-operator-in-javascript
JavaScript Instanceof Operator | GeeksforGeeks
December 27, 2023 - It returns a boolean value if true then it indicates that the object is an instance of a particular class and if false then it is not. Syntax:let gfg = objectName instanceof objectTypeParameters: objectName: S ... JavaScript String Operators ...
🌐
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 declare a variable called greeting and assign to this the string Hello. Next we check whether greeting is an instance of String. We get false. This may surprise you. When we use the instanceof operator we are checking whether it is an instance of the String object we are not checking the type.
🌐
Reality Ripple
udn.realityripple.com › docs › Web › JavaScript › Reference › Operators › instanceof
instanceof - JavaScript - UDN Web Docs: MDN Backup
The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value. The source for this interactive example is stored in a GitHub repository.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Symbol › hasInstance
Symbol.hasInstance - JavaScript | MDN - Mozilla
July 10, 2025 - Just in the same manner at which ... the instanceof keyword, we can also use Symbol.hasInstance for such checks. ... class Animal { constructor() {} } const cat = new Animal(); console.log(Animal[Symbol.hasInstance](cat)); // true ... This page was last modified on Jul 10, 2025 by MDN contributors. View this page on GitHub • Report ...
Top answer
1 of 3
30

What is the best way to recognize that obj2 and obj3 are different?

That will depend a great deal on what you're doing with them. One way would be to use instanceof Obj2 and instanceof Obj3. Since both objects were created with Obj1.prototype in their prototype chain, it makes sense that they identify as being an instance of what we would call the supertype in class-based OOP.

How does actually instanceof work?

The short version

obj instanceof F looks to see if the object referenced by F.prototype is anywhere in obj's prototype chain. It doesn't use constructor at all.

More details

This is covered in the spec by §11.8.5 - The instanceof Operator, which says (indirectly, via §8.6.2) that it calls the [[HasInstance]] internal method of the function object, passing in the object we're testing. Function's [[HasInstance]] (in §15.3.5.3) says that it gets the object reference from the function's prototype property and then returns true if that object is anywhere in the target object's prototype chain, false if it doesn't.

It doesn't use constructor (nothing in JavaScript itself does, in fact) — and if you think about it, it can't, because an object's constructor property can only point at one function, but an object can be instanceof multiple functions — for instance, in the case of pseudo-classical inheritance:

function F1() {}

function F2() {
  F1.call(this);
}
F2.prototype = Object.create(F1.prototype);
F2.prototype.constructor = F2;

var obj = new F2();
console.log(obj instanceof F1); // true
console.log(obj instanceof F2); // true

Both are true because the two objects referenced by F1.prototype and F2.prototype are both in obj's prototype chain.

instanceof being true doesn't necessarily mean that obj was created by a call to F, either directly or indirectly; it just indicates there's a vague link between them (F.prototype refers to an object that's also in obj's prototype chain). It usually means F was involved in creating the object, but there's no guarantee.

For instance:

function F() {}
var obj = Object.create(F.prototype);
console.log(obj instanceof F); // true

Note that F wasn't called to create the object, at all.


Or perhaps more clearly and/or dramatically:

function F() {}
var p = {};
var obj = Object.create(p);
console.log(obj instanceof F); // false
F.prototype = p;
console.log(obj instanceof F); // true


There's also this unusual, but entirely possible, version:

function F1() {}
function F2() {}
F1.prototype = F2.prototype = {};
var obj = new F1();
console.log(obj instanceof F2); // true


Or this one:

function F1() {}
function F2() {}
var obj = new F2();
console.log(obj instanceof F1); // false
F1.prototype = F2.prototype;
console.log(obj instanceof F1); // true

2 of 3
3

Most simply: obj instanceof constructor yields true when obj has constructor's prototype in it's constructor/prototype chain. In other words, your asking your engine whether obj can be treated like an instance of constructor / whether obj behaves like a constructor object.

There is a small handful of syntaxes that allow you to put constructor's prototype in obj's prototype chain. Any and all of them will cause obj instanceof constructor to be true. In your examples, both obj2 and obj3 have Obj1 in their prototype chain.

So, when you ask your javascript engine whether either obj2 or obj3 behave like an instance of Obj1, JavaScript assumes true -- the only case wherein they wouldn't is if you've overridden Obj1's behavior down the line.