JS: “instanceof” Operator

By Xah Lee. Date: . Last updated: .

JavaScript has a instanceof operator. The syntax is:

obj instanceof f

[see JS: Value Types]

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

[see JS: Prototype and Inheritance]

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

[see JS: 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
const root = Object.create ( null);

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

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

const obj1 = Object.create ( root ); // obj1's parent is root
const obj2 = Object.create ( obj1 ); // obj2's parent is obj1
const 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;
};

const 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 result.

// 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
const 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
const 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 JS: Determine Type of Object]

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

[see JS: Object.prototype.isPrototypeOf]

Don't use property key "constructor".

[see JS: Property Key “constructor”]

JS Constructor/Class

  1. “this” Binding
  2. What's Constructor?
  3. Property Key "prototype"
  4. Operator “new”
  5. “instanceof” Operator
  6. Property Key “constructor”
  7. Class
  8. Keyword “extends”
  9. Keyword “super”

JS Types Topic

  1. JS: Value Types
  2. JS: Primitive Value
  3. JS: Object Type
  4. JS: “typeof” Operator
  5. JS: Determine Type of Object
  6. JS: Object.prototype.isPrototypeOf
  7. JS: Property Key “constructor”
  8. JS: “instanceof” Operator
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.