FrontendAtlas
Interview Questions
Home>JavaScript interview questions> JavaScript coding challenges >Implement `instanceof`

Implement `instanceof`

intermediate
By FrontendAtlas Editorial · Updated Jul 14, 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.
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

Solution

Overview

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.

1

Approach: Prototype chain traversal (recommended)

First validate the documented ordinary-constructor contract: the right-hand side must be callable and its prototype must be an object or function. Throw TypeError for either invalid case. Then return false for a null or primitive left value. For an object/function left value, set target = Constructor.prototype and walk upward with Object.getPrototypeOf; return true on identity with the target and false at null. This runs in O(depth) time and O(1) space. It intentionally does not implement custom Symbol.hasInstance or bound-function forwarding.

export default function myInstanceOf(obj, Constructor) {
  if (typeof Constructor !== 'function') {
    throw new TypeError('myInstanceOf: Constructor must be a function');
  }

  const targetProto = Constructor.prototype;
  if (targetProto === null || (typeof targetProto !== 'object' && typeof targetProto !== 'function')) {
    throw new TypeError('myInstanceOf: Constructor must have an object prototype');
  }

  if (obj === null || obj === undefined) return false;

  const t = typeof obj;
  if (t !== 'object' && t !== 'function') return false;

  let current = Object.getPrototypeOf(obj);
  while (current !== null) {
    if (current === targetProto) return true;
    current = Object.getPrototypeOf(current);
  }

  return false;
}
export default function myInstanceOf(obj: any, Constructor: Function): boolean {
  if (typeof Constructor !== 'function') {
    throw new TypeError('myInstanceOf: Constructor must be a function');
  }

  const targetProto: any = (Constructor as any).prototype;
  if (targetProto === null || (typeof targetProto !== 'object' && typeof targetProto !== 'function')) {
    throw new TypeError('myInstanceOf: Constructor must have an object prototype');
  }

  if (obj === null || obj === undefined) return false;

  const t = typeof obj;
  if (t !== 'object' && t !== 'function') return false;

  let current: any = Object.getPrototypeOf(obj);
  while (current !== null) {
    if (current === targetProto) return true;
    current = Object.getPrototypeOf(current);
  }

  return false;
}

Notes & Pitfalls

Pitfalls
  • Checking `obj.__proto__` directly is non-standard and can be missing; use `Object.getPrototypeOf`.
  • Validate the callable right-hand side and its object/function prototype before the primitive-left early return required by this helper's contract.
  • Invalid ordinary constructor inputs throw TypeError instead of silently returning false.
  • This does not implement custom `Symbol.hasInstance` behavior or bound-function forwarding.
Edge cases
  • Functions are objects too: `myInstanceOf(function(){}, Function)` should be `true`.
  • Objects created with `Object.create(null)` have a `null` prototype chain; should generally return `false` for most constructors.
  • `Constructor.prototype` can be replaced after object creation; the helper checks the current prototype reference.
  • A callable whose prototype is null, undefined, or primitive is rejected by this ordinary-constructor contract.
Techniques
  • Prototype chain traversal with `Object.getPrototypeOf`.
  • Validate the ordinary constructor before guarding `null` and primitive left values.
  • Compare by reference equality (`===`) on prototypes.

Common mistakes on this challenge

  • myInstanceOf should walk prototype chain with strict guards

    Correct behavior depends on constructor validation and iterative prototype traversal.

    • Before inspecting the left value, throw `TypeError` unless the right-hand side is callable with an object/function prototype.
    • After that right-hand-side validation, return false for primitives, `null`, and `undefined`.
    • Walk `Object.getPrototypeOf(obj)` chain and compare against `Constructor.prototype`.

Resources

  • MDN – instanceof
  • MDN – Object.getPrototypeOf

Similar questions

Create Object with PrototypeeasyImplement the `new` OperatorhardShallow Clone (Object or Array)easy

Guides

Frontend interview preparation guideGuideFrontend coding interview questions and prep guideBlueprintJavaScript Problems That Actually Show UpBlueprintBuild Great UI in 60 MinutesBlueprint

Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.

Open frontend interview questionsBrowse JavaScript interview questionsOpen Essential 60Open Machine Coding HubOpen Frontend Coding ChallengesOpen System DesignOpen Interview Prep GuideOpen System Design BlueprintOpen Framework Prep PathsOpen Study PlansOpen JavaScript mastery study planOpen Company PrepOpen JavaScript Framework Prep Guide
↗Incidents hub
15 / 88