In JavaScript, the operator namely instanceof
is helpful to check the object’s type at run time. It returns true
if the object is an instance of a specific constructor otherwise the return value will be false
.

Syntax
objectName instanceof constructor
Parameters
objectName
: it shows the object name.
Return value
It returns the Boolean value i.e. true
or false
. If the object is the instance of a specific constructor true will be returned otherwise false.
Example
Let’s discuss an example to testify the type of object by using the instanceof
operator in JS.
<!DOCTYPE html>
<html>
<body>
<h2 style="color:red">JS Example Code</h2>
<p id="test"></p>
<script>
var subjects= ["Maths", "Biology", "Physics"];
document.getElementById("test").innerHTML =
(subjects instanceof Object) + "<br>" +
(subjects instanceof Number) + "<br>" +
(subjects instanceof Array) + "<br>" +
(subjects instanceof String);
</script>
</body>
</html>
- Line#7: Here we have declared and initialized an array namely
"subjects"
. - Line#9: This part of the code checks whether the defined array is an instance of a class
Object
type or not. - Line#10: This
(subjects instanceof Number)
code checks whether the defined array is an instance of classNumber
. - Line#11:
(subjects instanceof Array)
checks whether the definedsubjects
is an instance of classArray
. - Line#12: This part of the code checks whether the defined array is an instance of a class
String
.
Output

Importance of instanceof
operation in JavaScript
Variable declaration in JavaScript does not define the type of the variable. For example, we declare the variable like var x
. This variable x
can be a number, array, string, or user-defined data type.
In various other languages like C++, we declare the variable in a way like an int a or double b, etc. we can check that the object is related to the specific type or not by using instanceof
operation in JavaScript. Thus, it is a useful operator of JavaScript.