The JavaScript instanceof operator checks whether an object’s prototype chain contains the prototype property of a constructor. It is commonly used to determine whether an object was created from a particular class or constructor function.
1. JavaScript instanceof Syntax
object instanceof Constructor
The expression returns a Boolean value: true if the constructor’s prototype is found in the object’s prototype chain, or false otherwise.
2. Basic instanceof Example
Arrays are objects whose prototype chain includes Array.prototype. Therefore, an array passes the instanceof Array check.
const fruits = ["Apple", "Banana", "Mango"];
console.log(fruits instanceof Array); // true
console.log(fruits instanceof Object); // true
console.log(fruits instanceof Date); // false
The array is also an instance of Object because Object.prototype appears farther up its prototype chain.
3. instanceof with Constructor Functions
The operator also works with constructor functions. When you create an object using new, the constructor’s prototype becomes part of that object’s prototype chain.
function Person(name) {
this.name = name;
}
const person1 = new Person("Rahim");
console.log(person1 instanceof Person); // true
console.log(person1 instanceof Object); // true
person1 is an instance of Person because Person.prototype is in its prototype chain.
4. instanceof with JavaScript Classes
JavaScript classes use the same prototype-based inheritance system. You can use instanceof to check objects created from a class.
class Car {
constructor(brand) {
this.brand = brand;
}
}
const myCar = new Car("Toyota");
console.log(myCar instanceof Car); // true
console.log(myCar instanceof Object); // true
5. instanceof and Inheritance
When a class extends another class, instances of the child class also pass an instanceof check for the parent class. This happens because the parent’s prototype is included in the child’s instance prototype chain.
class Animal {
eat() {
return "Eating...";
}
}
class Dog extends Animal {
bark() {
return "Woof!";
}
}
const dog = new Dog();
console.log(dog instanceof Dog); // true
console.log(dog instanceof Animal); // true
console.log(dog instanceof Object); // true
This is useful when checking whether an object belongs to a class hierarchy, rather than only checking its most specific class.
6. Checking Multiple Types
You can combine instanceof checks with logical operators to determine whether a value matches one of several supported types.
function printValue(value) {
if (value instanceof Date) {
console.log("This is a date:", value.toDateString());
} else if (value instanceof Array) {
console.log("This is an array:", value);
} else if (value instanceof Map) {
console.log("This is a map:", value);
} else {
console.log("Other value:", value);
}
}
printValue(new Date());
printValue([10, 20, 30]);
printValue(new Map());
7. instanceof Does Not Check Primitive Values
Primitive values such as strings, numbers, and booleans are not instances of their wrapper constructors. For example, a string literal is not an instance of String, although an explicitly created String object is.
console.log("Hello" instanceof String); // false
console.log(new String("Hello") instanceof String); // true
console.log(25 instanceof Number); // false
console.log(new Number(25) instanceof Number); // true
console.log(true instanceof Boolean); // false
In normal application code, prefer primitive strings, numbers, and booleans instead of their wrapper objects.
8. instanceof with Object.create()
Object.create() lets you choose an object’s prototype directly. If you use a constructor’s prototype as that prototype, the resulting object passes the corresponding instanceof check even if the constructor was not called.
function User() {}
const user = Object.create(User.prototype);
console.log(user instanceof User); // true
This demonstrates that instanceof checks the prototype chain. It does not prove that a constructor function actually ran or initialized the object.
9. Changing a Prototype Can Change instanceof Results
The result depends on the object’s current prototype chain. If you replace an object’s prototype, its instanceof result can change.
function First() {}
function Second() {}
const item = new First();
console.log(item instanceof First); // true
console.log(item instanceof Second); // false
Object.setPrototypeOf(item, Second.prototype);
console.log(item instanceof First); // false
console.log(item instanceof Second); // true
Changing prototypes at runtime is generally best avoided unless there is a clear reason, because it can make code harder to understand and may affect performance.
10. instanceof and Symbol.hasInstance()
JavaScript allows a class or function to customize how instanceof works by defining a static method named Symbol.hasInstance. The operator calls this method when it is available.
class EvenNumber {
static [Symbol.hasInstance](value) {
return typeof value === "number" && value % 2 === 0;
}
}
console.log(4 instanceof EvenNumber); // true
console.log(7 instanceof EvenNumber); // false
console.log("4" instanceof EvenNumber); // false
This example defines a custom membership test based on a number’s value rather than its prototype chain. Use this feature carefully because it changes the usual meaning of instanceof.
11. instanceof vs typeof
Both operators help identify values, but they answer different questions. typeof reports a value’s primitive type category (with some historical quirks), while instanceof normally checks a constructor’s prototype in an object’s prototype chain.
| Feature | typeof | instanceof |
|---|---|---|
| Purpose | Returns a type string | Checks prototype-chain membership |
| Result | String | Boolean |
| Primitive values | Useful for primitive checks | Usually false for primitives |
| Custom behavior | No equivalent hook | Can use Symbol.hasInstance |
| Example | typeof “hello” returns “string” | [] instanceof Array returns true |
12. instanceof vs Array.isArray()
For checking arrays, Array.isArray() is generally more reliable than instanceof Array, especially when values may come from another browser window or JavaScript realm. Different realms can have different Array constructors and prototypes.
const values = [1, 2, 3];
console.log(values instanceof Array); // true
console.log(Array.isArray(values)); // true
Use Array.isArray() when the specific question is whether a value is an array.
