Skip to solution
easyFrontend

What is `Error.isError()` and why is `instanceof Error` unreliable across realms?

241 views
01

Understand the problem

Question presented to candidate: "If a real Error object is created inside an iframe (or a Node vm context) and passed to your main page's code, does instanceof Error correctly identify it as an Error there? What would you use instead?"

What a strong answer should cover:

  • 📌 Interview term: a realm — a separate global execution environment with its OWN set of built-in constructors (Object, Array, Error, etc.) — an iframe, a Web Worker, or (in Node) a vm context each create a genuinely SEPARATE realm from the main one.
  • 📌 Interview term: the real, direct answer to the prompt — verified directly, using Node's own real vm module to construct a genuinely separate realm: an Error object created in that OTHER realm genuinely fails instanceof Error when checked against THIS realm's own Error constructor — instanceof compares against a SPECIFIC constructor's prototype, and the other realm's Error.prototype is a genuinely different object from this realm's.
  • 📌 Interview term: Error.isError(value) — the real, purpose-built fix: verified directly, it correctly identified the SAME cross-realm Error object as true, despite instanceof failing on the identical value — it checks a real, internal engine-level tag rather than comparing against one specific realm's prototype.
  • 📌 Interview term: Error.isError vs. plain objects/strings — verified directly: it correctly returns false for a plain object or a string that merely LOOKS error-like, confirming it is genuinely checking for a real Error internally, not just duck-typing based on a message property.
  • A precise answer names that this cross-realm problem is not unique to Error — the identical real issue affects instanceof Array/instanceof Date across realms too, which is exactly why Array.isArray() (this bank's own array-methods coverage references it) has existed as the correct, realm-safe check for arrays for far longer; Error.isError is genuinely the same real fix, just for errors specifically, and a much newer addition.

Clarifying questions expected:

  • None — this is a definitional/technical question; directly answering WHY instanceof fails (cross-realm prototype mismatch, not a general instanceof bug) is the strong signal.

Code / implementation expected: Yes — a real, genuine cross-realm test (using Node's own vm module to actually construct a separate realm) is the clearest, most convincing demonstration, rather than merely asserting the problem exists.

errores2025realm
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 JavaScript fundamentals interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The cross-realm failure below was actually reproduced using Node's own real vm module —

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal, genuine cross-realm reproduction (a real iframe as a separate realm) proving instanceof Error fails while Error.isError correctly identifies the same real Error
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 35 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track