Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript Instanceof Operator

  • Last Updated : 11 Sep, 2020

Below is the example of the Instanceof Operator.

  • Example:

    <!DOCTYPE html> 
            <h1 style="color:green">GeeksforGeeks</h1
            <p id="GFG"></p
                var a = ["Geeks", "for", "Geeks"]; 
                document.getElementById("GFG").innerHTML = 
                    (a instanceof Array) + "<br>" + 
                    (a instanceof Number); 


  • Output:

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.


var gfg = objectName instanceof objectType

objectName: States the name of Object.

Example-1: objectTypes.

<!DOCTYPE html>
        <h1 style="color:green">GeeksforGeeks</h1>
        <p id="GFG"></p>
            var fruits = ["Apple", "Mango", "Banana"];
            document.getElementById("GFG").innerHTML =
                (fruits instanceof Array) + "<br>" +
                (fruits instanceof Object) + "<br>" +
                (fruits instanceof String) + "<br>" +
                (fruits instanceof Number);


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

<!DOCTYPE html>
    <h1 style="color:green">GeeksforGeeks</h1>
    <p id="GFG"></p>
        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);


Supported Browsers:

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!