instanceof special operator JS Home  <<  JS Reference  <<  instanceof

Prototype chain evaluation.

Description

The instanceof special operator tests whether an object has the prototype property of a constructor in its prototype chain.

Syntax

Signature Description
object instanceof constructorThe instanceof special operator tests whether an object has the prototype property of a constructor in its prototype chain.

Parameters

Parameter Description
objectThe object to test.
constructorThe constructor function to test for.

Examples

Following are examples of the instanceof special operator.


// The instanceof operator.
function A(){} // constructor 1 
function B(){} // constructor 2

var o1 = new A();  
var o2 = new B(); 
var anArray = new Array(5); 

anArray[0] = o1.constructor + " " + o2.constructor;
anArray[1] = o1 instanceof A; // true Object prototype chain  
anArray[2] = o2 instanceof A; // false (B.prototype not in A.prototype chain)  
anArray[3] = o1 instanceof Object; // all objects in Object prototype chain  
anArray[4] = o2 instanceof Object; // all objects in Object prototype chain  

alert(anArray); 

Press the button below to action the above code: