Skip to solution
easyFrontend

How does instanceof work?

256 views
01

Understand the problem

Question presented to candidate: "How does the instanceof operator actually work internally, and what does it check?"

What a strong answer should cover:

  • instanceof checks whether a constructor's prototype property appears anywhere in an object's prototype chain -- it walks the chain, comparing each link against Ctor.prototype, until it finds a match or reaches null.
  • It is genuinely re-evaluated on every use, not cached -- reassigning a constructor's prototype after an instance already exists can change what instanceof reports for that pre-existing instance.
  • Primitives never satisfy instanceof for their wrapper type (for example "str" instanceof String is false), while an explicitly boxed wrapper object does.
  • instanceof is an overridable protocol, not hard-wired behavior -- a class can define a static Symbol.hasInstance method to fully customize what instanceof reports for it.
  • instanceof only works reliably within a single realm (the same global environment) -- a value from a different realm (a different iframe, a different vm context) can genuinely fail an instanceof check against the "same" built-in type, which is why Array.isArray exists as a cross-realm-safe alternative for arrays specifically.

Code / implementation expected: Yes -- a runnable snippet that reimplements instanceof by hand using nothing but Object.getPrototypeOf, and confirms it matches the real operator.

instanceof
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for precise JavaScript object-model interview questions. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result below was actually run on Node.js v24.19.0 — the printed outp

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSA hand-written instanceof reimplementation matches the real operator, plus the live-not-cached and Symbol.hasInstance proofs (run directly)
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 34 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track