Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

Instanceof Operator Operators in JavaScript

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.

Instanceof operator

Syntax of using instanceof is as follows

                            
object instanceof constructor
 
                     
                            
                        

Where object is an object (or an expression that evaluates to an object). constructor is anything that constructs an object. It could be a class or a constructor function.

There are several ways in which we can create an object in JavaScript.

For Example, the following function creates an object person using the constructor function Person.

                            
function Person(Name) {
  this.Name = Name;
}
 
let person = new Person("aida bugg");
                     
                            
                        

We want to know whether the Person constructor function created the instance person. This is where we use the instanceof operator. The following code checks if person is created from the Person constructor function.

                            
console.log(person instanceof Person); // true
                     
                            
                        

person Instanceof Object also returns true, this is because the instanceof operator searches the prototype chain of the person object.

                            
console.log(person instanceof Object); // true
                     
                            
                        

How it works

The Instanceof operator compares the prototype of the object with the Prototype Property of the constructor. If they are the same then it returns true. If not it continues to check its prototype chain of the object.

When we run the code person instanceof Person the instanceof operator compares its prototype with the Prototype property of the constructor function. Since they are equal it returns true.

                            
console.log(person.__proto__=== Person.prototype)  //true
                     
                            
                        

When we check person instanceof Object the instanceof compares the prototype property of the Object (Object.prototype) with the prototype of the person (person.__proto__) object. Since they are not equal it moves up the chain (person.__proto__.__proto__) and then compares it again with Object.prototype. Since they are equal it returns true.

                            
console.log(person.__proto__          === Object.prototype)  //false
console.log(person.__proto__.__proto__=== Object.prototype)  //true
                     
                            
                        

Classes

Classes are another way to create objects.

                            
class Person {
  constructor(name) {
    this.name = name;
  }
}
const person = new Person('Bill Gates');
 
 
console.log(person instanceof Person)       //true
console.log(person instanceof Object)       //true
 
                     
                            
                        

Object.setPrototypeOf

Note that we can change the prototype of an object after it is constructed.

In the following example, we create the person object from Person function. But later we change its prototype using the Object.setPrototypeOf to Animal function.

                            
function Person(name)  {
  this.name = name;
}
 
function Animal(name)  {
  this.name = name;
}
 
const person = new Person('Bill Gates');
 
console.log(person instanceof Person)       //true
console.log(person instanceof Animal)       //false
console.log(person instanceof Object)       //true
 
 
//Change the Prototype of person to Animal;
Object.setPrototypeOf(person, Animal.prototype)
 
console.log(person instanceof Person)       //false
console.log(person instanceof Animal)       //true
console.log(person instanceof Object)       //true
                     
                            
                        

Primitive Values

Primitive values are not objects and therefore do not have a Prototype Property. Hence instanceof will not work with them. You can use the Typeof operator for primitive values

                            
let literalString = 'This is a literal string';
let stringObject  = new String('String created with constructor');
 
literalString instanceof String;  // false, string literal is not a String
stringObject  instanceof String;  // true
                     
                            
                        

Instanceof and multiple context (e.g. frames or windows)

Scripts running in different windows or frames ( <iframe>) gets their own execution environments. This means that they have a different global objects, 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 & arrays inherit from the former.

                            
let literalString = 'This is a literal string';
let stringObject  = new String('String created with constructor');
 
literalString instanceof String;  // false, string literal is not a String
stringObject  instanceof String;  // true