Home » Programming » Javascript » Javascript Instanceof

JavaScript instanceof Operator – What it Does, How to Use It

This article will explain what the JavaScript instanceof operator does and how it can be used. Examples provided.

What does instanceof do?

The instanceof operator returns TRUE or FALSE depending on whether a given value or variable is of a certain type or class – it checks whether a value is an instance of a given object class or type.

The purpose of instanceof may seem confusing – you already have typeof, so what do you need instanceof for?

typeof will simply return a string containing the name of the type or class of the variable. In contrast, instanceof will return a boolean value confirming whether a given variable matches a given class at a programmatic level.

What is a Type/Class?

A variable type determines what kind of data it can store and what can be done for it. A variable type is defined by its class – which is the code that defines what data can be stored and the functions and associated behavior.

For example, string typed variables are sequences of characters that can be joined and split (think words and sentences), while numeric typed variables contain numeric values which can be used in arithmetic.

instanceof Syntax

The syntax for using instanceof is as follows:

OBJECT instanceof CONSTRUCTOR

Note that:

  • OBJECT is the variable or value you want to check
  • CONSTRUCTOR is the name of the class you want to check whether OBJECT is an instance of
  • A boolean TRUE or FALSE value will be returned
  • instanceof checks the entire prototype chain of the OBJECT
    • This means that instanceof can return a positive result for two different CONSTRUCTORs if one constructor is derived from the other

instanceof JavaScript Examples

This example creates a Person class, and then an object is created using this class. instanceof is then used to confirm that the created object is of the Person class.

// Define a constructor which creates an object of the Person class - containing a person's details
function Person(name, age, height) {
    this.name = name;
    this.age = age;
    this.height = height;
}

// Define a new person using the above constructor
var jim = new Person('Jim', 21, 182);

// Check whether the jim variable is an instance of the Person class
console.log(jim instanceof Person);
// Will return true as jim is an instance of the Person class

// Check whether the jim variable is of the Object class 
console.log(jim instanceof Object);
// Will also return true - JavaScript objects are an inheritable class which other objects are built on

console.log(jim instanceof String);
// Will return false as the jim variable is not of the String type

Note that the jim object is considered an instance of both the Person and Object classes – as the Person class extends the Object class. In most cases, custom classes in JavaScript will extend the Object class when created unless otherwise specified.

For more examples on how instanceof can be used, check out the Mozilla developer documentation.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment