How to Check an Error's Type with instanceof in JavaScript
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
<value> instanceof <ClassName>
| Part | What 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. |
| Result | true 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:

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
node main.js
true
false
true

true:JSON.parsethrows aSyntaxErrorfor invalid JSON.false: it isn't aTypeError, a different kind of error.true: every built-in error type,SyntaxErrorincluded, is built onError, 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
| Mistake | What happens | Do this instead |
|---|---|---|
Throwing a string, as in throw 'oops' | err instanceof Error is false, and there's no stack trace | Throw new Error('oops') |
Checking only instanceof Error when you meant one kind | Unrelated errors, such as bugs, are handled as if expected | Check the specific class |
Comparing err.name === 'SyntaxError' | Works for built-in errors, but a subclass with its own name fails it | Use instanceof, which follows the class hierarchy |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
instanceof Error is false for something you caught | A string or plain object was thrown | Find the throw and make it throw an Error |
| An expected error isn't matched | It's a different class than you thought | Print 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.
