Latest Code Tutorials

Javascript Instanceof Operator Example | Instanceof in Javascript


Javascript instanceof is an inbuilt operator that tests whether a prototype property of the constructor appears anywhere in the prototype chain of an object. The instanceof operator examines the presence of constructor.type in the object‘s prototype chain. The instanceof operator returns the boolean value that indicates if an object is an instance of the particular class.

Javascript Instanceof Operator

The instanceof operator is used to check the type of the object at a run time. The instanceof operator tests a presence of constructor.prototype in the object‘s prototype chain.

It checks the current object and returns true if the object is of the specified object type.

The Left Hand Side (LHS) operand is an actual object being tested to the Right Hand Side (RHS) operand, which is an actual constructor of a class.

See the following syntax.

object instanceof constructor

The object is to be tested, and the constructor is the function to test against.

See the following example.

let str = 'The website is appdividend'; 
let myString  = new String();
let nStr    = new String('String created with constructor');
let mDate    = new Date();
let mObj     = {};
let mNonObj = Object.create(null);

console.log(str instanceof String)
console.log(myString instanceof String)
console.log(nStr instanceof String)
console.log(mDate instanceof Date)
console.log(mObj instanceof Object)
console.log(mNonObj instanceof Object)

See the following output.

➜  es git:(master) ✗ node app

In the above example, you can see that if we have used the constructor, then it returns true otherwise false. One thing worth mentioning is that instanceof evaluates to a true value if the object inherits from the class’s prototype.

Also, as you have already noticed, String(value) and new String(value) do the totally different things: If you call the constructor function of the built-in types without using the new operator, they try to convert the (‘cast’) an argument to a specific type. If you use the new operator, they create the wrapper object.

See the below code.

let e = new Employee("Eleven");
e instanceof Employee

That is e instanceof Employee is true since e inherits from Employee.prototype.

#Function Prototype

Let’s see another example.

// app.js

function App(name, price, size) { = name;
  this.price = price;
  this.size = size;
let oz = new App('oz', 15, 1024)
console.log(oz instanceof App);
console.log(oz instanceof Object);

In the above example, we have created a function Prototype called App, which will act as a constructor, and that is why it returns true when we create an object from the function prototype.

When es6 was not there, this is how you need to create a kind of class. See the following output.

➜  es git:(master) ✗ node app

Every object in the JavaScript has a prototype, accessible through a __proto__ property. Functions also have the prototype property, which is an initial __proto__ for the objects created by them. When the function is created, it is given the unique object for the prototype. An instanceof operator uses this uniqueness to provide you with an answer.

It’s worth noting that instanceof is defined by the use of the “new” keyword when declaring the object.

Specifying “new” actually copied the end state of a String constructor function into the variable, rather than just setting it to the return value. I think this better shows what the new keyword does.

#instanceof Array Built-in Functions

See the following example of Array.

let arr = [1, 2, 3];

console.log(arr instanceof Array);
console.log(arr instanceof Object);

See the following output.

➜  es git:(master) ✗ node app

Please note that the arr also belongs to the Object class. That’s because Array prototypically inherits from Object.

#Javascript instanceof number

Strings and numbers are the primitive datatypes, not the objects and arrays. Therefore they don’t have a [[Prototype]], so it’ll only work if you wrap them in the regular objects (called ‘boxing’ in Java).

That is why if we check on the number, then it will return false.

See the following example.

let num = 21

console.log(num instanceof Number);

See the following output.

➜  es git:(master) ✗ node app

For primitives, it will return their type in lower case, for objects, it will return their class in title case.

If you don’t want to discern between primitive values and wrapper objects (for whatever, probably bad reason), use typeof() function.

Finally, Javascript Instanceof Operator Example | Instanceof in Javascript is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.