JavaScript: “instanceof” Operator

By Xah Lee. Date: . Last updated: .

JavaScript has a instanceof operator. The syntax is:

obj instanceof f

〔►see JavaScript: Data Types

obj instanceof f returns true if obj is a child (or child of child …) of the value of f.prototype .

〔►see JavaScript: Prototype and Inheritance

obj instanceof f is basically the same as f.prototype.isPrototypeOf(obj).

〔►see JavaScript: Object.prototype.isPrototypeOf

ECMAScript 2015 §ECMAScript Language: Expressions#sec-instanceofoperator

// shows how
// o instanceof f
// is basically same as
// (f.prototype).isPrototypeOf (o)

// define a object with no parent
var root = Object.create ( null);

// define a function
function ff () {};

// set the property key "prototype"
ff.prototype = root;

var obj1 = Object.create ( root ); // obj1's parent is root
var obj2 = Object.create ( obj1 ); // obj2's parent is obj1
var obj3 = Object.create ( obj2 ); // obj3's parent is obj2

console.log ( obj3 instanceof ff ); // true

console.log ( Object.prototype.isPrototypeOf.call ( ff.prototype, obj3 ) ); // true

Here's a typical use of instanceof:

// “normal” example of using instanceof

function FF () {
    this.p = 3;
};

var f1 = new FF();

console.log( f1 instanceof FF);              // true
console.log(FF.prototype.isPrototypeOf(f1)); // true

Don't Use 「instanceof」

Tip: Avoid using the instanceof operator, because it is not a reliable way to find a object's constructor or type.

(there's no reliable way to find a object's constructor. Objects are often created without constructor, and object's parent can be changed anytime.)

Here's examples where instanceof returns unexpected results.

// example showing 「instanceof」 isn't really about constructor. It's about prototype chain

// create a constructor function FF
function FF () {return {};}; // returns a object

// create a instance
var f1 = new FF();

console.log( f1 instanceof FF);              // false
console.log(FF.prototype.isPrototypeOf(f1)); // false
// example of 「instanceof」 with unexpected result

// create a constructor function FF
function FF () {
    this.p = 3;
};

// create a instance
var f1 = new FF();

// change property "prototype"
FF.prototype = [];

console.log( f1 instanceof FF);              // false
console.log(FF.prototype.isPrototypeOf(f1)); // false

If you want to find the subtype of a object, use Object.prototype.toString.call(obj)

〔►see JavaScript: Determine Type of Object

If you want to determine if a object is in the prototype chain of another, use isPrototypeOf.

〔►see JavaScript: Find Object's Prototype

Avoid using property key "constructor" too.

〔►see JavaScript: Property Key "constructor"

JS Constructor Topic

  1. JavaScript: “this” Binding
  2. JavaScript: What's Constructor?
  3. JavaScript: Property Key "prototype"
  4. JavaScript: Operator “new”
  5. JavaScript: “instanceof” Operator
  6. JavaScript: Property Key "constructor"
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.