instanceof in JavaScript: classes, functions, numbers, and the prototype chain

javascript instanceof, js instanceof, instanceof javascript, instanceof js, instanceof, instanceof in javascript, instance of javascript, instance of js, javascript instanceof function, javascript instanceof number: prototype chain, primitives, boxed values, cross-realm caveat.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

instanceof in JavaScript: classes, functions, numbers, and the prototype chain

The instanceof operator answers whether a value’s prototype chain includes the prototype property of a constructor function or class. People look it up as javascript instanceof, instanceof javascript, js instanceof, instanceof js, bare instanceof, or with filler words like instanceof in javascript, instance of javascript, and instance of js. Two specialized intents show up as javascript instanceof function and javascript instanceof number—covered below with real Function values and primitive versus boxed numbers.

instanceof inspects objects, not typeof strings: it walks [[Prototype]] until it finds a match or returns false. That makes it ideal for custom classes and builtins, but a poor fit for primitives and a fragile fit for objects created in another realm (another iframe or worker), where the constructor reference on the right-hand side may not live in the same global object.

Tested on: Node.js v20.18.2. A short note after each runnable snippet describes what you should see in the console.


Classes and inheritance

Diagram: myCar links through Car.prototype and Vehicle.prototype toward Object.prototype, matching instanceof checks along the chain

javascript
class Vehicle {}
class Car extends Vehicle {}

const myCar = new Car();
console.log(myCar instanceof Car);
console.log(myCar instanceof Vehicle);
Output

You should see 2 lines, in order: true, true.


Constructor functions versus object literals

Objects created with new inherit the constructor’s prototype. Plain object literals only match Object (and other builtins up the chain), not your custom constructor.

javascript
function Cat(name) {
  this.name = name;
}

const cat1 = new Cat("Fluffy");
console.log(cat1 instanceof Cat);
console.log(cat1 instanceof Object);
const cat2 = { name: "Fluffy" };
console.log(cat2 instanceof Cat);
console.log(cat2 instanceof Object);
Output

You should see 4 lines, in order: true, true, false, true.


Built-in constructors

javascript
const arr = [1, 2, 3];
console.log(arr instanceof Array);
console.log(arr instanceof Object);
const date = new Date(0);
console.log(date instanceof Date);
console.log(date instanceof Object);
const error = new Error("Something went wrong");
console.log(error instanceof Error);
console.log(error instanceof Object);
Output

You should see 6 lines, in order: true, true, true, true, true, true.

For arrays coming from another window, prefer Array.isArray because it does not depend on reference identity of the Array constructor.


javascript instanceof number: primitives versus boxed values

Diagram: typeof and instanceof results differ for primitive numbers versus Number objects created with new

Primitive numbers are not instances of Number; object-wrapped numbers are.

javascript
console.log(1 instanceof Number);
console.log("foo" instanceof String);
console.log(true instanceof Boolean);
Output

You should see 3 lines, in order: false, false, false.

javascript
console.log(new Number(1) instanceof Number);
console.log(Object(1) instanceof Number);
Output

You should see 2 lines, in order: true, true.


javascript instanceof function

Function objects are both Function and Object instances.

javascript
function f() {}
console.log(f instanceof Function);
console.log(f instanceof Object);
Output

You should see 2 lines, in order: true, true.


Symbol primitive versus boxed symbol

A naked symbol is a primitive, so instanceof Symbol is false; Object(sym) boxes it.

javascript
const sym = Symbol("a");
console.log(sym instanceof Symbol);
console.log(Object(sym) instanceof Symbol);
Output

You should see 2 lines, in order: false, true.


Cross-realm caveat (read before shipping iframe code)

If the left-hand value was constructed in a different JavaScript realm, its prototype may point at that realm’s %Array.prototype% while your Array identifier points at the current realm’s constructor. instanceof Array can then return false even for real arrays—Array.isArray, Object.prototype.toString, or structured-clone-safe checks are more reliable.


Summary

Javascript instanceof operator questions center on whether a value’s prototype chain includes a constructor’s .prototype—great for custom classes and many built-ins, but wrong for primitive literals ("text" instanceof String is false) unless boxed. Cross-realm iframes are the other big FAQ: instanceof Array can fail even for real arrays when the constructor identity differs between realms, which is why Array.isArray exists.

Use typeof for primitive discrimination, instanceof for most custom object hierarchies, and Object.prototype.toString.call when you need a stable tag across boundaries. Document whichever approach you pick so future readers do not “fix” working code by swapping checks blindly.


References

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …