JavaScript instanceof Operator

The instanceof operator in JavaScript is utilized to determine the type of an object during runtime. The result produced can be a Boolean value, either true or false, depending on the inputs provided and the objects involved.

Syntax:

Example

var instance = objectName instanceof objectType

Parameters

ObjectName: The name of the object.

ObjectType: This identifies the type of an object, which is utilized for validation purposes.

Why is an instanceof necessary in JavaScript?

The instanceof operator in JavaScript is essential because when we define a variable such as XYZ, which can represent various user-defined data types, it can serve multiple functions, including integers, characters, arrays, and several other data types. Consequently, this operator assists us in determining the specific data type to which an object is associated.

Example 1: The Instanceof Operator and Inheritance

In this section, we will explore several instances that pertain to the instanceof operator:

Example

<!DOCTYPE html>
<html>
<head>
<title>Example for instanceof operator</title>
<style>
h1
{
text-align:center;
color:green;
}
.i
{
color:blue;
border-style:solid;
border-color:black;
border-width:3px;
font-size:19px;
}
</style>
</head>
<body>
<h1>Example for Array Instanceof Operator</h1>
<div class="i">
<script>
var courses = ["Java", "Python", "HTML","CSS","JavaScript"]; 
document.write("Does the given courses are of array type?-"+(courses instanceof Array) + "<br>"); 
document.write("Does the given courses are of number type?-"+(courses instanceof Number) + "<br>");
</script>
<div>
</body>
</html>

Output

Justification

  • Initially, a course array containing a few course values was created.
  • Next, we verified that the object of the course was an instance of the array type; since courses are an array type, we were provided with the true value.
  • After that, we verified that the object of the course was an instance of the Number type; since courses are not a Number type, the result was false.
  • Example 2: Symbol.hasInstance

Similar to how the instanceof operator can be utilized to determine whether an object is an instance of a specific class, Symbol.hasInstance can be leveraged to verify if a class encompasses a given object instance. When an object belongs to the class, Symbol.hasInstance will yield true; if not, it will return false.

Example

class Lang {
  constructor() {}
}

const Java = new Lang();

// Checking if Lang contains any instance of Java or not
console.log(Lang[Symbol.hasInstance](Java));

Output

Justification

  • We started by creating a class called Lang and adding a constructor to it.
  • Next, we made an object called Java and set its initial state to be the class Lang instance.
  • Lastly, we have used the Symbol.hasInstance method to determine whether or not the class Lang contains an instance of the object Java.
  • Example 3: Making use of String with instanceof

In this example, we will explore how the instanceof operator works with String objects.

Example

const sentence = "Today is Monday";
// Creating a string object which will be an instance of a String
const string_object = new String("String created with constructor");

// Using the instanceof operator to check 
console.log(sentence instanceof String); 
console.log(string_object instanceof String); 

console.log(sentence instanceof Object); 
console.log(string_object instanceof Object); 

console.log(string_object instanceof Date);

Output

Justification

  • We started a sentence in the first step by initializing it with "Today is Monday."
  • Next, a string object was made.
  • Ultimately, we have verified which object belongs to which class.
  • Example 4: Making use of Date and instanceof

In this example, we will explore how the instanceof operator functions when applied to Date objects.

Example

// Creating new_date object which will be an instance of Date
const new_date = new Date();

// Using the instanceof the operator to check 
console.log(new_date instanceof Date); 
console.log(new_date instanceof String); 
console.log(new_date instanceof Object); 
console.log(new_date instanceof Number);

Output

Justification

  • In the initial step, we instantiated an object of the Date class and assigned it the identifier new_date.
  • Finally, we confirmed the class of which the object is an instance.
  • Example 5: Using Object.create to Create Objects

In this instance, we will investigate how the instanceof operator functions when utilizing the Object.create method to generate the object.

Example

const obj1 = {};
// Creating null_object which will be a NULL object
const null_object = Object.create(null);
null_object.name = "Null Object";

// Using the instanceof operator to check 
console.log(obj1 instanceof Object); 
console.log({} instanceof Object); 
console.log(null_object instanceof Object);

Output

Example 6: Not an instanceof

In this illustration, we will explore how to verify if a given object is an instance of a specific class by utilizing the Not operator in combination with the instanceof operator.

Example

function Class1(){}
function Class2(){}
// Creating obj object which will be an instance of Class2
var obj = new Class2();
console.log(obj instanceof Class1);
console.log(obj instanceof Object);

// Checking if the obj is an instance of Class1 or not
if (!(obj instanceof Class1)) {
    // Now making obj object an instance of Class1
    obj = new Class1();
}

// Using the instance of operator to check 
console.log(obj instanceof Class1);

Output

Justification

  • Two functions, Class1 and Class2, were created in the first step and are both empty.
  • Next, we made an object called obj and assigned it to be a Class2 instance.
  • Next, we ascertained which object belongs to which class.
  • Next, by applying the (!) inside the if condition, we have utilized the not an instanceof method by placing a not (!) operator in front of the instanceof method, we can make the object an instance of Class1 if the condition is met.
  • In summary

From the article, we learned the following:

  • To determine an object's type at runtime, use the JavaScript instanceof operator.
  • If the object is an instance of a specific class, the JavaScript instanceof operator will return true; otherwise, it will return false.
  • ObjectName and ObjectType are the two parameters that the JavaScript instanceof operator takes as arguments.
  • If the ObjectType is not an object, the JavaScript instanceof operator will throw an exception.

Input Required

This code uses input(). Please provide values below: