Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
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. Its behavior can be customized with Symbol.hasInstance.
The object to test.
constructorConstructor to test against.
Thrown if constructor is not an object. If constructor doesn't have a [Symbol.hasInstance]() method, it must also be a function.
The instanceof operator tests the presence of constructor.prototype in object's prototype chain. This usually (though not always) means object was constructed with constructor.
Note that the value of an instanceof test can change if constructor.prototype is re-assigned after creating the object (which is usually discouraged). It can also be changed by changing object's prototype using Object.setPrototypeOf.
Classes behave in the same way, because classes also have the prototype property.
For bound functions, instanceof looks up for the prototype property on the target function, since bound functions don't have prototype.
If constructor has a Symbol.hasInstance method, the method will be called in priority, with object as its only argument and constructor as this.
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. This means that they have different built-ins (different global object, different constructors, etc.). This may result in unexpected results. For instance, [] instanceof window.frames[0].Array will return false, because Array.prototype !== window.frames[0].Array.prototype and arrays in the current realm inherit from the former.
This may not make sense at first, but for scripts dealing with multiple frames or windows, and passing objects from one context to another via functions, this will be a valid and strong issue. For instance, you can securely check if a given object is in fact an Array using Array.isArray(), neglecting which realm it comes from.
For example, to check if a Node is an SVGElement in a different context, you can use myNode instanceof myNode.ownerDocument.defaultView.SVGElement.
The following example shows the behavior of instanceof with String objects.
The following example shows the behavior of instanceof with Map objects.
The following example shows the behavior of instanceof with objects created using Object.create().
The following code creates an object type Car and an instance of that object type, myCar. The instanceof operator demonstrates that the myCar object is of type Car and of type Object.
To test if an object is not an instanceof a specific constructor, you can do:
This is really different from:
This will always be false. (!myCar will be evaluated before instanceof, so you always try to know if a boolean is an instance of Car).
A common pitfall of using instanceof is believing that, if x instanceof C, then x was created using C as constructor. This is not true, because x could be directly assigned with C.prototype as its prototype. In this case, if your code reads private fields of C from x, it would still fail:
To avoid this, you can override the behavior of instanceof by adding a Symbol.hasInstance method to C, so that it does a branded check with in:
Note that you may want to limit this behavior to the current class; otherwise, it could lead to false positives for subclasses:
You could do this by checking that this is the current constructor:
| ECMAScript® 2027 Language Specification # sec-relational-operators |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 8, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.