Object.prototype.isPrototypeOf()

Tests whether the calling object appears anywhere in another object's prototype chain.

Signature
isPrototypeOf(object)
  • Added in JavaScript ES3

Parameters

ParameterTypeDescription
$objectanyThe object whose prototype chain will be searched.

Return value

booleantrue if the calling object lies in the prototype chain of the given object, otherwise false; false when object is not an object.

Errors

Throws a TypeError if the this value is null or undefined; a primitive argument simply returns false.

isPrototypeOf() walks the argument's prototype chain and reports whether the receiver is somewhere on it. It answers the same question as instanceof but from the other direction, and without needing a constructor function.

Direction matters

Read it as "is the thing on the left an ancestor of the thing on the right": Animal.prototype.isPrototypeOf(dog). Beginners reliably write it backwards. Note the .prototype — Animal.isPrototypeOf(dog) compiles fine and is essentially always false, because a class object is not in its instances' chain.

When to reach for it over instanceof

  • No constructor exists. Objects built with Object.create(proto) have a prototype but nothing to put on the right of instanceof.
  • You have the prototype, not the class. instanceof requires a callable with a .prototype; arrow functions have none and throw.
  • Checking against Object.prototype to see whether a value is an ordinary object rather than a null-prototype dictionary.

What trips people up

Primitives always return false, even when the boxed form would inherit from the receiver: String.prototype.isPrototypeOf('x') is false because no boxing occurs on the argument.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Check ancestry for objects created with Object.create(), which have no constructor to test
  • Verify a value inherits from Object.prototype before assuming toString() exists
  • Assert a class hierarchy relationship when only the prototype object is in scope

Frequently asked questions

What is the difference between isPrototypeOf and instanceof?

instanceof takes a constructor on the right and checks its .prototype against the chain, while isPrototypeOf() is called directly on the prototype object, so it works when no constructor exists.

Why does MyClass.isPrototypeOf(obj) return false?

The class object itself is not in the instance's prototype chain; you need MyClass.prototype.isPrototypeOf(obj).

Why does it return false for a string or a number?

Primitives are not coerced to wrapper objects for this check, so any primitive argument returns false; use instanceof with the boxed value or check typeof instead.

More examples

Ancestry without a constructor

There is no constructor function here, only two plain objects linked by Object.create(). isPrototypeOf() handles that directly, while instanceof throws because its right-hand side must be callable.

Example · javascript
const shape = { sides: 0 };
const square = Object.create(shape);
square.sides = 4;

console.log(shape.isPrototypeOf(square));   // true
console.log(square.isPrototypeOf(shape));   // false

try {
  console.log(square instanceof shape);
} catch (err) {
  console.log("threw:", err.constructor.name); // threw: TypeError
}

Detecting a null-prototype object

Both values are objects with the same data, but only one inherits from Object.prototype. The check predicts exactly which one has toString() available, so it is a reliable guard before any code that relies on inherited object methods or on string coercion.

Example · javascript
const plain = { a: 1 };
const bare = Object.create(null);
bare.a = 1;

console.log(Object.prototype.isPrototypeOf(plain));  // true
console.log(Object.prototype.isPrototypeOf(bare));   // false

console.log(typeof plain.toString);   // "function"
console.log(typeof bare.toString);    // "undefined"
console.log(String(plain));            // "[object Object]"

Discussion

  • Be the first to comment on this lesson.