Skip to content

instanceof

In JavaScript, the instanceof keyword is a binary operator that checks whether an object’s prototype chain includes a constructor’s prototype property. It returns a boolean, and the right operand is usually a class or constructor function.

Because the check walks the whole prototype chain, an instance of a subclass created with extends is also an instance of its parent class and of Object. Primitives aren’t objects, so "text" instanceof String is false.

The right operand must be an object. If it doesn’t define a [Symbol.hasInstance]() method, it also has to be callable, or instanceof throws a TypeError. A class can define a static [Symbol.hasInstance]() method to customize the result. An array from another realm, such as an iframe or a Node.js vm context, inherits from that realm’s Array.prototype, so instanceof Array returns false for it and Array.isArray() is the reliable test.

JavaScript instanceof Keyword Syntax

The instanceof operator takes an object on its left and a constructor on its right:

Language: JavaScript Syntax
<object> instanceof <constructor>

JavaScript instanceof Keyword Examples

The instanceof operator checks an object against each class in its prototype chain:

Language: JavaScript
> class Shape {}
undefined
> class Circle extends Shape {}
undefined
> const wheel = new Circle();
undefined

> wheel instanceof Circle
true
> wheel instanceof Shape
true
> wheel instanceof Array
false

> "text" instanceof String
false

> wheel instanceof {}
Uncaught TypeError: Right-hand side of 'instanceof' is not callable

The subclass instance matches both classes in its chain. A string primitive isn’t an instance of String, and a plain object on the right throws.

JavaScript instanceof Keyword Use Cases

  • Checking which class created an object before calling its methods
  • Telling custom error classes apart in a catch block
  • Narrowing a value that could be one of several object types
  • Customizing type checks with a static [Symbol.hasInstance]() method

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 4, 2026