Implement instanceof

intermediate
By FrontendAtlas Editorial · Updated Oct 10, 2026
Implement the ordinary prototype-chain core of instanceof: first require a callable right-hand side with an object/function prototype, then return false for a null or primitive left-hand side, and otherwise walk toward Constructor.prototype. Invalid ordinary constructor inputs throw TypeError. Custom Symbol.hasInstance dispatch and bound-function internals are outside this exercise, so this is not a complete replacement for the operator.

Arguments

  • obj: any — The value to check (may be an object or a primitive).
  • Constructor: Function — The constructor function whose .prototype should be searched in the prototype chain.

Returns

boolean — true if Constructor.prototype is in obj's prototype chain; otherwise false.

Solution idea

For an ordinary constructor, the core prototype test asks whether Constructor.prototype appears in the left value's prototype chain. Validate this helper's callable-RHS and object/function-prototype contract before returning false for primitive left values, then repeatedly call Object.getPrototypeOf until the target or null. Native instanceof also dispatches custom Symbol.hasInstance methods and has special bound-function behavior; both are explicitly outside this scoped exercise.

Examples
// Example 1
function Person() {}
const p = new Person();
myInstanceOf(p, Person); // => true

// Example 2
myInstanceOf({}, Array); // => false
myInstanceOf([], Array); // => true

// Example 3: primitives
myInstanceOf(1, Number); // => false
myInstanceOf('a', String); // => false
myInstanceOf(null, Object); // => false