Skip to main content

How to Check an Error's Type with instanceof in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A catch block receives every error, whatever went wrong. To react only to the kind you expect, such as invalid JSON, check the error's type with instanceof, which also knows that a specific error type belongs to a more general one.

Syntax​

Syntax
<value> instanceof <ClassName>
PartWhat it does
<value>The value to test, such as the error from catch (err).
<ClassName>The class to test against, such as SyntaxError or Error.
Resulttrue when <value> was made by <ClassName> or by a class built on it; otherwise false.

MDN's instanceof reference: "The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value."

The Example​

The example makes JSON.parse fail on text that isn't JSON, then asks three questions about the error it caught: is it a SyntaxError, a TypeError, an Error?

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which calls JSON.parse on text that isn't JSON in a try block and, in the catch block, prints err instanceof SyntaxError, err instanceof TypeError and err instanceof Error.

main.js
try {
JSON.parse('{ not json }');
} catch (err) {
console.log(err instanceof SyntaxError);
console.log(err instanceof TypeError);
console.log(err instanceof Error);
}

Step 2: Run It​

Terminal
node main.js
Output
true
false
true

A zsh terminal where node main.js prints true, false and true.

  • true: JSON.parse throws a SyntaxError for invalid JSON.
  • false: it isn't a TypeError, a different kind of error.
  • true: every built-in error type, SyntaxError included, is built on Error, so the general check matches too.

The command is the same in zsh, bash and PowerShell.

Why the Base Class Matters​

Libraries often define one base error class and several specific ones built on it. Checking the base class catches all of them at once, while checking a specific class catches only that one. Use the general check when every failure from that library should be handled the same way, and the specific one when only one failure is expected.

Common Mistakes​

MistakeWhat happensDo this instead
Throwing a string, as in throw 'oops'err instanceof Error is false, and there's no stack traceThrow new Error('oops')
Checking only instanceof Error when you meant one kindUnrelated errors, such as bugs, are handled as if expectedCheck the specific class
Comparing err.name === 'SyntaxError'Works for built-in errors, but a subclass with its own name fails itUse instanceof, which follows the class hierarchy

Troubleshooting​

SymptomLikely causeFix
instanceof Error is false for something you caughtA string or plain object was thrownFind the throw and make it throw an Error
An expected error isn't matchedIt's a different class than you thoughtPrint err.name and err.constructor.name to see its class

FAQ​

What should I do with errors I don't expect?​

Throw them again with throw err;, so they aren't hidden by a catch meant for something else.

Does instanceof work with my own classes?​

Yes. new MyError() instanceof MyError is true, and so is instanceof Error when MyError extends Error.

Conclusion​

err instanceof SomeError tells you which kind of error you caught. It matches the class itself and every class built on it, so instanceof Error matches every real error.

Additional Resources​

  • instanceof, on MDN.
  • Error, on MDN, with the list of built-in error types.