JavaScript instanceof Operator
You are Here:
JavaScript instanceof Operator
The instanceof
operator returns a Boolean value indicates whether the prototype property of a constructor appears anywhere in the prototype chain of an object.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var num = new Number(36);
var str = new String("hello");
var bool = new Boolean(true);
var arr = [1, 2, 3];
var obj = {bike: "honda", color: "red"};
var txt = "";
txt += "num : "+ (num instanceof Number) + "<br>";
txt += "str : "+ (str instanceof String) + "<br>";
txt += "bool : "+ (bool instanceof Boolean) + "<br>";
txt += "arr : "+ (arr instanceof Array) + "<br>";
txt += "obj : "+ (obj instanceof Object) + "<br>";
document.write(txt);
</script>
</body>
</html>
Syntax
object instanceof constructor
Parameter Values
Value | Type | Explanation |
---|---|---|
object | Required | The object to test. |
constructor | Required | Function to test against. |
Return Values
Value | Explanation |
---|---|
true | If the object is the instance of constructor. |
false | If the object is not the instance of constructor. |
More Examples
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
function bike(brand, model, color){
this.brand = brand;
this.model = model;
this.color = color;
}
var honda = new bike("Honda", "CB1000R", "Red");
document.write(honda instanceof bike);
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.