Table of contents
1.
Introduction
2.
instanceof
3.
instanceof vs typeof
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

How does instanceOf work difference with typeof

Author Malay Gain
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In javascript, though it seems like typeof and instanceof are similar, they are different operators, and they have different use-cases. Typeof operator checks the primitive type of a value, whereas instanceof checks if the value is an instance of a class or class function. Here we will look into how instanceof works and its differences with typeof operator.

instanceof

instanceof operator checks if the constructor’s prototype appears anywhere in the object’s prototype chain. It returns a boolean value, true or false.

In simple words, it can be said that if a value is an instance of a class or class function, it returns true otherwise false.

Syntax

object instanceof class

Here, the object is the value that needs to be checked, and the class is the class or function to check against. 

Example

class Student
{
    constructor(name, enr_no)
    {
        this.name=name;
        this.enr_no=enr_no;
    }
};
var student1 = new Student("Ninja",510519);
console.log(student1 instanceof Student);
You can also try this code with Online Javascript Compiler
Run Code

Output

true

Use cases

instanceof for prototype checking 

instanceof operator checks the presence of constructor.prototype in object’s prototype chain.

class Student
{
    constructor(name, enr_no)
    {
        this.name=name;
        this.enr_no=enr_no;
    }
};
function Scholar() {}
Scholar.prototype = new Student();
let obj= new Scholar();
console.log(obj instanceof Student); 
// Student.prototype is now in obj's prototype chain
You can also try this code with Online Javascript Compiler
Run Code

Output

true

Using with strings

String literals are not instance of a String class but a string created with a string constructor is an instance of a string class.

var str1 = 'Coding Ninjas';
//string literal not instance of String class
console.log(str1 instanceof String); 
var str2 =new String('Coding Ninjas Studio');
console.log(str2 instanceof String); 
You can also try this code with Online Javascript Compiler
Run Code

Output

false
true

Object created using Object.create() function 

instanceof operator also works for object created using Object.create( ) function.

Let’s visualize this through examples

class Student
{
    constructor(name, enr_no)
    {
        this.name=name;
        this.enr_no=enr_no;
    }
};
function Scholar() {}
Scholar.prototype = Object.create(Student.prototype);
let obj= new Scholar();
console.log(obj instanceof Student); 
// Student.prototype is now in obj's prototype chain
You can also try this code with Online Javascript Compiler
Run Code

Output

true

instanceof vs typeof

typeof operator checks if value is of type boolean or string or number or function or object.

Syntax

//it returns primitive type of the value as a string
typeof value
var str='Coding Ninjas'
console.log(typeof str)
You can also try this code with Online Javascript Compiler
Run Code

Output

string

FAQs

1. Which operator is used to check undefined value?

typeof operator is used to check undefined value.

2. What are object prototypes?

In javascript, every object has a built-in property that is called its prototype. The prototype is itself an object, so the prototype has its own prototype, i.e., prototype chain. The prototype chain ends when we reach a prototype that has null for its own prototype.

Object.getPrototypeOf( ) 
//for finding the prototype of an object
You can also try this code with Online Javascript Compiler
Run Code

Key Takeaways

This article covered instanceof, typeof operator and their difference

Check out the Coding Ninjas Studio library for getting a better hold of the data structures and algorithms and the guided path for learning javascript from basics.

Side by side, you can also practice a wide variety of coding questions commonly asked in interviews in Coding Ninjas Studio. Along with coding questions, you can also find the interview experience of scholars working in renowned product-based companies here. 
Check out this problem - Longest String Chain

Happy learning!

Live masterclass