Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

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);

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

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); 

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

Output

true
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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)

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

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!

Previous article
Creating and Publishing your First npm Package
Next article
JavaScript Projects for Beginners
Live masterclass