courses.reviews logo
I launched a free website to help you find the best courses with reviews & discounts.
Up to date
Published
3 min read

Trevor I. Lasn

Staff Software Engineer, Engineering Manager

Error.isError(): A Better Way to Check Error Types in JavaScript

Why the new Error.isError() method solves important cross-realm issues and provides more reliable error identification than instanceof

JavaScript’s error handling system has long had a blind spot when dealing with errors across different execution contexts. The new Error.isError() method addresses this limitation, providing developers with a more reliable way to identify error objects.

The traditional approach to checking if a value is an Error has been using the instanceof operator.

JavaScript
try {
// Code that might throw
} catch (e) {
if (e instanceof Error) {
// Handle error
}
}

This approach has two significant limitations:

  1. Cross-realm errors aren’t correctly identified. When an error originates from another realm (like an iframe or VM module), instanceof Error returns false because each realm has its own Error constructor.

This can lead to situations where errors are not handled correctly, as the instanceof check fails.

  1. Fake errors can pass the test. Objects with Error.prototype in their prototype chain but lacking error characteristics will be incorrectly identified as errors.

These issues can lead to inconsistent error handling and difficult-to-diagnose bugs. The new Error.isError() method provides a solution:

Instead of checking the prototype chain, Error.isError() uses a simpler and more reliable approach. It looks for a special internal marker (like a hidden ID tag) that gets added to every genuine Error object when it’s created.

This method works better than instanceof for two reasons:

  1. It correctly identifies errors even when they come from different contexts (like iframes or modules)
  2. It rejects fake objects that try to pretend they’re errors by manipulating the prototype

Think of it like checking for a manufacturer’s watermark instead of just looking at the label - it’s much harder to fake.

Typing Error.isError()

Here’s one way you could type the Error.isError() method in TypeScript:


Found this article helpful? You might enjoy my free newsletter. I share dev tips and insights to help you grow your coding skills and advance your tech career.

Interested in supporting this blog in exchange for a shoutout? Get in touch.


Liked this post?

Check out these related articles that might be useful for you. They cover similar topics and provide additional insights.

Javascript
6 min read

setImmediate() vs setTimeout() in JavaScript

both setImmediate() and setTimeout() are used for scheduling tasks, but they work differently.

Sep 8, 2024
Read article
Javascript
4 min read

Embrace Intermediate Variables and Early Returns in JavaScript

Early returns and intermediate variables make your code easier to reason about

Aug 30, 2024
Read article
Javascript
6 min read

AggregateError in JavaScript

Handle multiple errors at once

Sep 2, 2024
Read article
Javascript
7 min read

JavaScript Sets and Maps: Beyond Arrays and Objects

How to handle unique values and key-value pairs properly without type coercion and performance issues

Nov 17, 2024
Read article
Javascript
9 min read

Exploring JavaScript Symbols

Deep dive into JavaScript Symbols - what they are, why they matter, and how to use them effectively

Nov 15, 2024
Read article
Javascript
7 min read

JavaScript Truthy and Falsy: A Deep Dive

Grasp JavaScript's type coercion with practical examples and avoid common pitfalls

Oct 27, 2024
Read article
Javascript
3 min read

Float16Array in JavaScript

Understanding the new 16-bit floating point array in JavaScript

Apr 14, 2025
Read article
Javascript
4 min read

Promise.try: Unified Error Handling for Sync and Async JavaScript Code (ES2025)

Stop mixing try/catch with Promise chains - JavaScript's new Promise.try handles return values, Promises, and errors uniformly

Nov 10, 2024
Read article
Javascript
7 min read

WeakRefs in JavaScript: Explained In Simple Terms

Understanding how WeakRef helps manage memory in JavaScript

Jan 7, 2025
Read article

This article was originally published on https://www.trevorlasn.com/blog/error-iserror-javascript. It was written by a human and polished using grammar tools for clarity.