Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Error.isError()

Limited availability

TheError.isError() static method determines whether the passed value is anError.

Syntax

js
Error.isError(value)

Parameters

value

The value to be checked.

Return value

true ifvalue is anError; otherwise,false.

Description

Error.isError() checks if the passed value is anError. It does so by performing abranded check for a private field initialized by theError() constructor.This is the same mechanism used byArray.isArray(), which is in turn similar to the mechanism used by thein operator.

It is a more robust alternative toinstanceof Error because it avoids false positives and false negatives:

  • Error.isError() rejects values that aren't actualError instances, even if they haveError.prototype in their prototype chain —instanceof Error would accept these as it does check the prototype chain.
  • Error.isError() acceptsError objects constructed in another realm —instanceof Error returnsfalse for these because the identity of theError constructor is different across realms.

Error.isError() returnstrue forDOMException instances. This is because, althoughDOMException is not specified as a real subclass ofError (theError constructor is not the prototype of theDOMException constructor),DOMException still behaves likeError for all branded checking purposes.

Examples

Using Error.isError()

js
// all following calls return trueError.isError(new Error());Error.isError(new TypeError());Error.isError(new DOMException());try {  1 + 1n;} catch (e) {  console.log(Error.isError(e)); // The operation threw a TypeError, so this returns true}// all following calls return falseError.isError();Error.isError({});Error.isError(null);Error.isError(undefined);Error.isError(17);Error.isError("Error");Error.isError(true);Error.isError(false);// This is not an error, because the object does not have the private field// initialized by the Error constructorError.isError({ __proto__: Error.prototype });

instanceof vs. Error.isError()

When checking forError instance,Error.isError() is preferred overinstanceof because it works across realms.

js
const iframe = document.createElement("iframe");document.body.appendChild(iframe);const xError = window.frames[window.frames.length - 1].Error;const error = new xError();// Correctly checking for ErrorError.isError(error); // true// The prototype of error is xError.prototype, which is a// different object from Error.prototypeerror instanceof Error; // false

Normalizing caught errors

You can useError.isError() to detect if the caught value is an error and normalize it to an error object.

js
try {  throw "Oops; this is not an Error object";} catch (e) {  if (!Error.isError(e)) {    e = new Error(e);  }  console.error(e.message);}

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-error.iserror

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp