js instance of code example
Example 1: instanceof javascript
var color = "red";
var color2 = {};
color instanceof String
color2 instanceof Object
Example 2: js class check if new instance
new Date() instanceof Date;
Example 3: insanceof
The instanceof operator tests to see if the prototype property of a constructor
appears anywhere in the prototype chain of an object. The return value is a
boolean value.
For example :-
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
const auto = new Car('Honda', 'Accord', 1998);
console.log(auto instanceof Car);
console.log(auto instanceof Object);
Example 4: how to check instance
The instanceof keyword checks whether an object
is an instance of a specific class or an interface.
The instanceof keyword compares the
instance with type. The return value
is either true or false .