Thursday, September 4, 2025
HomeLanguagesJavascriptJavaScript Instanceof Operator

JavaScript Instanceof Operator

The instanceof operator in JavaScript is used to check the type of an object at run time. It returns a boolean value if true then it indicates that the object is an instance of a particular class and if false then it is not. 

Syntax:

var gfg = objectName instanceof objectType

Parameters: This method accepts a single parameter.

  • objectName: States the name of the Object.

Return Value: This method returns a boolean value if true then it indicates that the object is an instance of a particular class and if false then it is not. 

Example 1: Below is the example of the Instanceof Operator.

html




<h1 style="color:green">
    neveropen
</h1>
<h3>
    Instanceof Operator.
</h3>
 
<p id="GFG"></p>
 
<script>
    var a = ["Geeks", "for", "Geeks"];
     
    document.getElementById("GFG").innerHTML =
        (a instanceof Array) + "<br>" +
        (a instanceof Number);
</script>


Output: 

JavaScript Instanceof Operator

JavaScript Instanceof Operator

 Example 2: 

html




<h1 style="color:green">
    neveropen
</h1>
<h3>
    Instanceof Operator.
</h3>
 
<p id="GFG"></p>
 
<script>
    var fruits = ["Apple", "Mango", "Banana"];
     
    document.getElementById("GFG").innerHTML =
        (fruits instanceof Array) + "<br>" +
        (fruits instanceof Object) + "<br>" +
        (fruits instanceof String) + "<br>" +
        (fruits instanceof Number);
</script>


Output:

JavaScript Instanceof Operator

JavaScript Instanceof Operator

 Example 3: Demonstrating that String and Date objects are also a type of Object (derived from Object). 

Javascript




<script>
    var myString = new String();
    var myDate = new Date();
     
    console.log(myString instanceof Object);
    console.log(myString instanceof Date);
    console.log(myString instanceof String);
    console.log(myDate instanceof Date);
    console.log(myDate instanceof Object);
    console.log(myDate instanceof String);
</script>


Output:

true
false
true
true
true
false

We have a complete list of Javascript Operators, to check those please go through the Javascript Operators Complete Reference article.

Supported Browsers:

  • Google Chrome
  • Firefox
  • Edge
  • Opera
  • Apple Safari
RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS