---
title: "Error.isError() in JavaScript: A Reliable Error Check"
description: "Why the new Error.isError() method solves important cross-realm issues and provides more reliable error identification than instanceof"
canonical: "https://www.trevorlasn.com/blog/error-iserror-javascript"
author: "Trevor I. Lasn"
published: "2025-05-09"
last-updated: "2025-05-09"
topics: ["JavaScript"]
---

# Error.isError() in JavaScript: A Reliable Error Check

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

By Trevor I. Lasn · Published 2025-05-09 · https://www.trevorlasn.com/blog/error-iserror-javascript

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.

[Browser support for `is-error` on webstatus.dev](https://webstatus.dev/features/is-error)

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

```js title="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.

```js title="JavaScript"
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
const iframeError = iframe.contentWindow.Error;

// error in the iframe realm
const crossRealmError = new iframeError('Error from iframe');

// This check fails, even though it's clearly an error
console.log(crossRealmError instanceof Error); // false 🔴

// Error.isError() correctly identifies it
console.log(Error.isError(crossRealmError)); // true 🟢
```  

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

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

```js title="JavaScript"
// "fake" error by setting Error.prototype in the prototype chain
const fakeError = { message: "I'm not a real error" };
Object.setPrototypeOf(fakeError, Error.prototype);

// This incorrectly identifies it as an Error
console.log(fakeError instanceof Error); // true 🔴

// Error.isError() correctly rejects it
console.log(Error.isError(fakeError)); // false 🟢
```

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

```js title="JavaScript"
try {
  // Code that might throw
} catch (e) {
  if (Error.isError(e)) {
    // Handle error with confidence
  }
}
```

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:

```ts title="TypeScript"
try {
} catch (e: unknown) {        // Code that might throw
  if (Error.isError(e)) {     // Here 'e' is narrowed to type Error
    console.log(e.message);   // This is now type-safe
  } else {                    // Handle non-Error exceptions
    console.log("Non-error was thrown:", e);
  }
}
```
