instanceof

instanceof — Used to determine whether a variable is an instantiated object of a certain prototype.

Description

object instanceof prototype constructor

Parameters

Name Description Type Default Optional
constructor The prototype to check against. prototype No

Return values

boolean.

Examples

Example #1 – instanceof example
class Animal {} class Cat extends Animal {} class Dog extends Animal {} var a = new Animal(); var c = new Cat(); var d = new Dog(); console.log(a instanceof Animal); // true console.log(c instanceof Animal); // true console.log(d instanceof Animal); // true console.log(a instanceof Dog); // false console.log(c instanceof Dog); // false console.log(d instanceof Dog); // true
Example #2 – instanceof example
var a = new String('one'); var b = 'two'; console.log(a instanceof String); // true console.log(b instanceof String); // false console.log(a instanceof Object); // true console.log(b instanceof Object); // false

External references