Javascript-instanceOf
The instanceof operator in JavaScript is defined to check the type of an object at run time. It calls a boolean values 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:
objectName: States the name of Object.
Example:
objectTypes.
<!DOCTYPE html>
<html>
<body>
<center>
<h1 style="color:yellow">I2TUTORIALS</h1>
<p id="i2"></p>
<script>
var fruits = ["Apple", "Mango", "Banana"];
document.getElementById("i2").innerHTML =
(fruits instanceof Array) + "<br>" +
(fruits instanceof Object) + "<br>" +
(fruits instanceof String) + "<br>" +
(fruits instanceof Number);
</script>
</center>
</body>
</html>
OUTPUT:
I2TUTORIALS true true false false
Example:
String and Date objects
<!DOCTYPE html> <html> <body> <h1 style="color:yellow">I2Tutorials</h1> <p id="i2"></p> <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> </body> </html>
OUTPUT:
I2Tutorials Console LOg true false true true true false
Supported Browsers:
Google Chrome
Firefox
Edge
Opera
Apple Safari