Check if a Variable is of type Error in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
3 min

banner

# Check if a Variable is of type Error in JavaScript

Use the instanceof operator to check if a variable is of type Error, e.g. err instanceof Error.

The instanceof operator will return true if the variable stores an instance of the Error class.

index.js
const err = new Error('๐Ÿ’ฃ๏ธ Something went wrong'); console.log(err instanceof Error); // ๐Ÿ‘‰๏ธ true

check if variable is of type error

The code for this article is available on GitHub

We used the Error constructor to create a new error object.

We then used the instanceof operator to check if the err variable has the prototype property of the Error constructor in its prototype chain.

index.js
const err = new Error('๐Ÿ’ฃ๏ธ Something went wrong'); class Person {} console.log(err instanceof Error); // ๐Ÿ‘‰๏ธ true console.log(err instanceof Person); // ๐Ÿ‘‰๏ธ false if (err instanceof Error) { console.log('The variable is of type error'); } else { console.log('The variable is NOT of type error'); }

using instanceof operator to check if variable is error

# Using instanceof with custom Errors

This approach would also work if you create custom errors and extend the Error constructor.

index.js
class NotFoundError extends Error { constructor(message) { super(message); this.name = 'NotFoundError'; this.status = 404; } } const err = new NotFoundError('product not found'); console.log(err instanceof Error); // ๐Ÿ‘‰๏ธ true console.log(err instanceof NotFoundError); // ๐Ÿ‘‰๏ธ true

using instanceof with custom errors

The code for this article is available on GitHub

We created a NotFoundError class that extends from the Error class.

Using the instanceof operator returns true for both the Error and NotFoundError classes.

However, if you throw an error in an IFrame (inline frame) in your application, the instanceof check might fail, if you do it in a parent window.

As an alternative, you can use duck-typing.

index.js
function isError(error) { if (error && error.stack && error.message) { return true; } return false; } const err = new Error('๐Ÿ’ฃ๏ธ Something went wrong'); console.log(isError(err)); // ๐Ÿ‘‰๏ธ true console.log(isError('test')); // ๐Ÿ‘‰๏ธ false console.log(isError({})); // ๐Ÿ‘‰๏ธ false

using duck typing to check if variable is an error

A simple way to think about duck-typing is that we're basically saying:

An error has stack and message properties. If the value also has a stack and message properties, then it must be an error.

This approach works when using IFrames because we check if the argument we passed to the function has the properties an error would have.

Where this might go wrong is if we have an object, that is not an error, but also has the stack and message properties.

index.js
function isError(error) { if (error && error.stack && error.message) { return true; } return false; } // ๐Ÿ‘‡๏ธ๏ธ true console.log(isError({stack: 1, message: 'test'}));
The code for this article is available on GitHub

In the code sample, even though the object we passed to the isError function is not an error, it satisfies both conditions, so the function returns true and gives us a false positive.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev