References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript instanceof operator

Operator JavaScript All modern browsers Updated
Quick answer

The instanceof operator checks whether an object was created from a given class or constructor. [] instanceof Array is true; new Date() instanceof Date is true. It tests the object's prototype chain, so it also returns true for parent classes. Use typeof for primitive types instead.

Overview

instanceof answers "did this object come from that class?" obj instanceof ClassName returns true if ClassName's prototype appears anywhere in obj's prototype chain. It's how you tell apart objects of different types — an Error from a plain object, a Date from a string, your own class instances from others.

Because it walks the whole prototype chain, it respects inheritance: if Admin extends User, then admin instanceof User is also true. That's usually what you want — an admin is a kind of user. It pairs naturally with class and is common in catch blocks to branch on the error type (if (err instanceof TypeError)).

The division of labor with typeof is clean: typeof is for primitives (strings, numbers, booleans), instanceof is for objects and class instances. One caveat — instanceof can give wrong answers across different execution contexts like iframes, because each has its own copy of the built-ins; for detecting an array reliably in any context, Array.isArray() is safer.

Syntax

object instanceof Constructor

[] instanceof Array            // true
new Date() instanceof Date     // true
new Date() instanceof Object   // true (chain)
"hi" instanceof String         // false (primitive)

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  class Animal {}
  class Dog extends Animal {}

  const d = new Dog();

  document.getElementById('out').textContent =
    'Dog?    ' + (d instanceof Dog) + '\n' +
    'Animal? ' + (d instanceof Animal) + '\n' +
    'Array?  ' + (d instanceof Array);
  // Dog? true / Animal? true / Array? false
</script>

Best practices

  • Use instanceof for objects and class instances; use typeof for primitives.
  • Branch on error types in catch blocks, e.g. if (err instanceof TypeError).
  • Remember it checks the whole prototype chain, so it's true for parent classes too.
  • For reliable array detection (including across iframes), prefer Array.isArray().

Frequently asked questions

What does instanceof do in JavaScript?
It returns true if an object was created from a given constructor or class, by checking the object's prototype chain.
What is the difference between typeof and instanceof?
typeof names a primitive type as a string; instanceof checks whether an object descends from a particular class or constructor.
Why is "hello" instanceof String false?
Because string literals are primitives, not String objects. Use typeof "hello" === "string" to check for a string.
Does instanceof respect inheritance?
Yes. If Child extends Parent, a Child instance is instanceof both Child and Parent.