JavaScript Error.isError() Method

JavaScript Tutorials


JavaScript Error.isError() checks whether a value is an actual Error object. It provides a standardized brand check that is more reliable than testing only with instanceof Error, especially when values can come from another realm such as an iframe.

The method is part of modern ECMAScript. It returns true for Error instances and built-in error subclasses, and false for ordinary objects that only look like errors.

Error.isError() Syntax

const result = Error.isError(value);
  • value is the value you want to test.
  • The method returns true when the value has the internal Error brand.
  • It returns false for primitives and ordinary objects.

Error.isError() checks the Error brand instead of only walking the prototype chain. This is why it can behave more reliably than instanceof Error.

Check Different Values

The following example checks real errors and values that only resemble errors.

Example:

const values = [
  new Error("Network failed"),
  new TypeError("Invalid value"),
  { message: "Looks like an error" },
  "Error"
];

// Check each value using the standardized Error brand check.
for (const value of values) {
  console.log(Error.isError(value));
}

Output:

true
true
false
false

Error.isError() vs instanceof Error

The instanceof operator asks whether Error.prototype appears in an object's prototype chain. That works for many ordinary cases, but it has two important limitations.

First, an object can be given Error.prototype without being a genuine Error instance. Such an object may pass an instanceof check even though it was not created as an Error.

Second, objects created in another JavaScript realm have different constructor and prototype identities. An Error created inside an iframe can therefore fail an instanceof Error check in the parent page.

Check What it tests Cross-realm behavior
Error.isError(value) Internal Error brand Designed to recognize genuine errors across realms
value instanceof Error Error.prototype in the prototype chain Can fail for errors created in another realm

Use It in a catch Block

JavaScript allows any value to be thrown. A catch block might receive an Error object, a string, a number, or another value. Error.isError() helps you normalize that value before reading Error-specific properties.

Example:

try {
  throw "Service unavailable";
} catch (caught) {
  // Convert non-Error values into a normal Error.
  const error = Error.isError(caught)
    ? caught
    : new Error(String(caught));

  console.log(error.message);
}

Output:

Service unavailable

Built-in Error Subclasses

Built-in subclasses such as TypeError, RangeError, ReferenceError, SyntaxError, URIError, and AggregateError are still Error objects. Error.isError() therefore returns true for them.

Example:

const errors = [
  new TypeError("Wrong type"),
  new RangeError("Outside range"),
  new ReferenceError("Missing variable")
];

for (const error of errors) {
  console.log(Error.isError(error));
}

Output:

true
true
true

Objects That Only Look Like Errors

An object with name and message properties is not automatically an Error. Error.isError() can distinguish those plain objects from actual Error instances.

const fakeError = {
  name: "Error",
  message: "Something failed"
};

console.log(Error.isError(fakeError));

Output:

false

Browser Support

Error.isError() is a newer JavaScript feature. Modern engines may support it before older browsers and embedded web views do. If your application targets older environments, check runtime compatibility before depending on it without a fallback.

A compatibility fallback can still use instanceof Error for common same-realm cases, but it does not provide exactly the same semantics.

Common Mistakes

  • Using it to inspect an error type: Error.isError() tells you whether a value is an Error, not whether it is specifically a TypeError or RangeError.
  • Assuming every thrown value is an Error: JavaScript can throw any value, so checking caught values can still be useful.
  • Replacing validation with error checks: detecting an Error object does not tell you whether input data is valid or whether an operation should be retried.
  • Ignoring runtime support: a newer standard method can require a fallback in older environments.

Recommended Practices

  • Use Error.isError() when values may cross iframe or realm boundaries.
  • Normalize unknown caught values before accessing message, stack, or cause.
  • Use instanceof with a specific error class when you intentionally need subtype checks and realm boundaries are not a concern.
  • Keep user-facing error messages separate from internal diagnostic details.

Conclusion

Error.isError() gives JavaScript a direct, standardized way to identify genuine Error objects. It is especially useful for defensive error handling, cross-realm code, and normalization of unknown caught values. Use it when you need a stronger Error check than a plain object shape or a same-realm instanceof test.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram