The JavaScript instanceof operator is used to check the type of an object at the run time. The return value is a boolean value. The instanceof
operator tests the presence of constructor.prototype
in object
‘s prototype chain.
var myVar = objectName instanceof objectType
One thing worth mentioning is instanceof
evaluates to true if the object inherits from the class’s prototype:
var p = new Person("Jon");
p instanceof Person
That is p instanceof Person
is true since p
inherits from Person.prototype
.
JavaScript instanceof operator
A simple example code checks the current object and returns true if the object is of the specified object type.
<!DOCTYPE html>
<html>
<body>
<script>
var color1 = new String("green");
console.log(color1 instanceof String);
var color2 = "coral";
console.log(color2 instanceof String);
</script>
</body>
</html>
Output:

The Instanceof operator also takes inheritance into account. It returns true if the object inherits from the classes’ prototype.
Example code returns true if obj belongs to the Class or a class inheriting from it.
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
const auto = new Car('Honda', 'Accord', 1998);
console.log(auto instanceof Car);
// true
console.log(auto instanceof Object);
// true
Do comment if you have any doubts or suggestions on this JS Operator example.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.