instanceof

Tests whether a constructor's prototype appears in an object's prototype chain.

Since ES3 Spec ↗

Syntax

object instanceof Constructor

Returns

boolean — `true` if `Constructor.prototype` is in `object`'s prototype chain.

Throws

  • TypeError — The right-hand operand is not callable.

Examples

class Animal {}
class Dog extends Animal {}
const d = new Dog();
console.log(d instanceof Dog);
console.log(d instanceof Animal);
Output
true
true
console.log([] instanceof Array);
console.log([] instanceof Object);
Output
true
true
console.log("hi" instanceof String);
Output
false

Notes

- Primitive values are never `instanceof` their wrapper types. - Does not work reliably across different realms (e.g. iframes); use `Array.isArray()` for arrays. - Customizable via the `Symbol.hasInstance` method.

Browser & runtime support

EnvironmentSince version
chrome 1.0
firefox 1.0
safari 1.0
edge 12
node 0.10

See also