Path: blob/main/files/en-us/web/javascript/reference/operators/instanceof/index.md
6529 views
------{{jsSidebar("Operators")}}
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.
{{EmbedInteractiveExample("pages/js/expressions-instanceof.html")}}
Syntax
Parameters
object: The object to test.
constructor: Constructor to test against.
Exceptions
{{jsxref("TypeError")}}
: Thrown if
constructoris not an object. Ifconstructordoesn't have a@@hasInstancemethod, it must also be a function.
Description
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.
instanceof and @@hasInstance
If constructor has a Symbol.hasInstance method, the method will be called in priority, with object as its only argument and constructor as this.
instanceof and multiple realms
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 {{jsxref("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.
Examples
Using instanceof with String
The following example shows the behavior of instanceof with String objects.
Using instanceof with Date
The following example shows the behavior of instanceof with Date objects.
Objects created using Object.create()
The following example shows the behavior of instanceof with objects created using {{jsxref("Object.create()")}}.
Demonstrating that mycar is of type Car and type Object
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.
Not an instanceof
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).
Overriding the behavior of instanceof
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:
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
{{jsxref("Symbol.hasInstance")}}
{{jsxref("Object.prototype.isPrototypeOf")}}