Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

The instanceof operator

With an ordinary, unbound function on the right, the instanceof operator defaults to checking whether the function’s `prototype` occurs in an object’s prototype chain. It accounts for inheritance, but does not prove how an object was created or that particular fields exist.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

value instanceof Target

value instanceof Target returns a Boolean or throws an exception. In the default check, when value is an object and Target is an ordinary, unbound function, the operator searches the prototype chain of value for the object Target.prototype, comparing object identity. A derived-class instance is therefore also an instance of its base class. If Target is a bound function (for example, one created with bind), the built-in check repeats the instanceof test with the function it wraps instead of reading Target.prototype. No custom Symbol.hasInstance method is needed for this delegation. instanceof is a reserved word.

The test does not inspect the constructor property or the history of new calls. An object created with Object.create(Target.prototype) can pass without running the constructor. Changing prototypes can change the result; separate realms, such as frames, can have different prototype objects.

Target[Symbol.hasInstance] can customize the test; its result is converted to a Boolean. Without custom handling, primitives on the left produce false. A primitive on the right causes TypeError; without a suitable method, the right side must be callable. If the default object test reads Target.prototype and that value is not an object, it throws TypeError.

Write negation as !(value instanceof Target), not !value instanceof Target: the latter tests !value. Do not treat the result as proof that fields exist or data is valid.

Examples

class Animal {}
class Dog extends Animal {}

const dog = new Dog();
const linked = Object.create(Dog.prototype);

console.log(dog instanceof Dog);
console.log(dog instanceof Animal);
console.log(linked instanceof Dog);
console.log("dog" instanceof String);

Result

true
true
true
false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources