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
class Vehicle {}
class Car extends Vehicle {}
const myCar = new Car();
console.log(myCar instanceof Car);
console.log(myCar instanceof Vehicle);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.
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);You should see 4 lines, in order: true, true, false, true.
Built-in constructors
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);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
Primitive numbers are not instances of Number; object-wrapped numbers are.
console.log(1 instanceof Number);
console.log("foo" instanceof String);
console.log(true instanceof Boolean);You should see 3 lines, in order: false, false, false.
console.log(new Number(1) instanceof Number);
console.log(Object(1) instanceof Number);You should see 2 lines, in order: true, true.
javascript instanceof function
Function objects are both Function and Object instances.
function f() {}
console.log(f instanceof Function);
console.log(f instanceof Object);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.
const sym = Symbol("a");
console.log(sym instanceof Symbol);
console.log(Object(sym) instanceof Symbol);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.
