Skip to the solution
EasyFrontend6 min read

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

241 views

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.

  • error
  • es2025
  • realm

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, genuine cross-realm reproduction (a real iframe as a separate realm) proving instanceof Error fails while Error.isError correctly identifies the same real Error

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 35 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What is the difference between map and forEach?

Continue