JavaScript Instanceof Method

JavaScript Instanceof() Method with Examples

In this article, I am going to discuss JavaScript Instanceof() Method with Examples. Please read our previous article where we discussed JavaScript Object Nested Properties.

JavaScript Instanceof() Method

As we have learned about a JavaScript object that Objects are Reference data types. The typeof operator is used to identifying the type of objects we are using in the program hence it will return “object” for Arrays as well as Objects.

To find the datatype of an object, we use instanceof(Object) method. The instanceof() method will check whether the passed argument/value is an Object type or not. It returns a Boolean value(true/false). The JavaScript instanceof() method returns true for array, object, and function type. This means instanceof() method will check datatype of all reference datatypes such as Arrays, Objects, and Functions.

Syntax: var result = objectName instanceof objectType
Parameters: objectName: Specify the name of the object to check whether it’s an object type or not.

Example: JavaScript Objects, Instanceof() method to check the type of an object
<html>
<head>
    <title>JavaScript Objects, instanceof() method to check the type of an object example</title>
</head>
<body>
    <script>
        //Object Literal with properties
        var person = {
            firstName: "Elon",
            lastName: "Musk",
            age: 25
        };

        //display data
        console.log("person object: ", person)
        console.log("person object length: ", Object.getOwnPropertyNames(person).length)
        console.log("person Object literal type? : ", typeof person);// object
  console.log("Is person object is an instance of an Object : ", person instanceof Object);// true
        console.log("Is person object is an instance of an Array : ", person instanceof Array);// false
        console.log("Is person object is an instance of a String : ", person instanceof String);// false
        console.log("Is person object is an instance of a Number : ", person instanceof Number);// false
    </script>
</body>
</html>
Output:

JavaScript Objects, Instanceof() method to check the type of an object

In the above code when we are passing object name Person as a parameter which is an object to instanceof() method to check whether it is an instance of an object or not hence person instanceof Object it returns true because Object is the superclass of JavaScript everything comes under it even array too. That means Object is a parent class in JavaScript and everything is derived from an object only. As we already learned the instanceof () method returns true for array, object, and function type. Whereas, the code line person instanceof Array returns false as it’s not an array hence that is why when we write person instanceof String and this person instanceof Number it returns false as it is not a type of String and Number.

Similarly, way we can check the data type of an array, and also, we can demonstrate that it’s also a type of an object by using instanceof () method which returns true for the array as we have learned. What we discussed above is given in the below example.

Example: JavaScript Objects, instanceof() method to check the type of an array
<html>
<head>
    <title>JavaScript Objects, instanceof() method to check the type of an array example</title>
</head>
<body>
    <script>        
        //Array with items/elements
        var arrFruits = ["Apple", "Banana", "Kiwi"];

        //display data
        console.log("arrFruits array: ", arrFruits)
        console.log("arrFruits array length: ", arrFruits.length)//length of an array3
        console.log("arrFruits array type? : ", typeof arrFruits);// object
        console.log("Is arrFruits array is an instance of an Array : ", arrFruits instanceof Array);// true
        console.log("Is arrFruits array is an instance of an Object : ", arrFruits instanceof Object);// true
        console.log("Is arrFruits array is an instance of a String : ", arrFruits instanceof String);// false
        console.log("Is arrFruits array is an instance of a Number : ", arrFruits instanceof Number);// false

    </script>
</body>
</html>
Output:

JavaScript Objects, instanceof() method to check the type of an array

In the above code when we are passing array name arrFruits as a parameter which is an array to instanceof() method to check whether it is an instance of an array or not hence arrFruits instanceof Array it returns true. Also, the code line arrFruits instanceof Object returns true as it is also a type of an object because Object is the superclass of JavaScript everything comes under it even array too. That means Object is a parent class in JavaScript and everything is derived from an object only.

As we already learned the instanceof() method returns true for array, object, and function type. That is why when we write arrFruits instanceof String and this arrFruits instanceof Number it returns false as it is not a type of String and Number.

Similarly, way we can check the data type of a built-in function, and also, we can demonstrate that it’s also a type of an object by using instanceof () method which returns true for functions also as we have learned. What we discussed above is given in the below example.

Example: JavaScript Objects, instanceof() method to check the type of a function
<html>
<head>
    <title>JavaScript Objects, instanceof() method to check the type of a function example</title>
</head>
<body>
    <script>
        //JavaScript Built-in functions
        var myStr = new String();
        var myDate = new Date();

        //display data
        //Check mystr function data type
        console.log("Is myStr function is an instance of an Object : ", myStr instanceof Object);// true
        console.log("Is myStr function is an instance of a String : ", myStr instanceof String);// true
        console.log("Is myStr function is an instance of a Date : ", myStr instanceof Date);// false

        //Check myDate function data type
        console.log("Is myDate function is an instance of an Object : ", myDate instanceof Object);// true
        console.log("Is myDate function is an instance of a Date : ", myDate instanceof Date);// true
        console.log("Is myDate function is an instance of a String : ", myDate instanceof String);// false
        
    </script>
</body>
</html>
Output:

JavaScript Objects, instanceof() method to check the type of a function

In the above code when we are passing the built-in String function name myStr as a parameter which is a function to instanceof() method to check whether it is an instance of a String function or not hence myStr instanceof String returns true. Also, the code line myStr instanceof Object returns true as it is also a type of an object because Object is the superclass of JavaScript everything comes under it even array too. That means Object is a parent class in JavaScript and everything is derived from an object only.

As we already learned the instanceof () method returns true for array, object, and function type. That is why when we write myStr instanceof Date it returns false as it is not a type of Date.

The same goes to the built-in Date function when we pass function name myDate as a parameter which is a function to instanceof() method to check whether it is an instance of a Date function or not hence myDate instanceof Date returns true. Also, the code line myDate instanceof Object returns true as it is also a type of an object because Object is the superclass of JavaScript everything comes under it even array too. That means Object is a parent class in JavaScript and everything is derived from an object only.

As we already learned the instanceof () method returns true for array, object, and function type. That is why when we write myDate instanceof String it returns false as it is not a String type.

In the next article, I am going to discuss Accessing non-existent JavaScript Properties with Examples. Here, in this article, I try to explain JavaScript Instanceof() Method with Examples and I hope you enjoy this JavaScript Instanceof() Method article.

Leave a Reply

Your email address will not be published. Required fields are marked *