Skip to main content

What types of errors exist in JavaScript?

1. Error - the base error class

This is the universal type that all other errors inherit from. If you are not sure which error type fits, you can just use Error.

javascript
throw new Error("Something went wrong!");

Properties:

  • name - the error's name ("Error")
  • message - the description
  • stack - the stack trace (shows where the error occurred)

2. ReferenceError - a reference error

Occurs when there is a reference to a non-existent variable or a property that is not accessible in the current scope.

javascript
console.log(x); // ReferenceError: x is not defined

It can also occur from incorrect use of this.

3. TypeError - a type error

Occurs when an operation is applied to a value of the wrong type, or when something that is not a function is called.

javascript
let n = null; console.log(n.toString()); // TypeError: Cannot read properties of null
javascript
let func = 123; func(); // TypeError: func is not a function

4. RangeError - out of the allowed range

Occurs when a passed value is outside the allowed range.

javascript
new Array(-5); // RangeError: Invalid array length
javascript
(123.456).toFixed(200); // RangeError: toFixed() digits argument must be between 0 and 100

5. SyntaxError - a syntax error

An error caused by violating JavaScript syntax. The engine cannot "parse" the code, so the program does not even start.

javascript
eval("if (true { console.log('hi'); }"); // SyntaxError: Unexpected token '{'

Such errors cannot be caught with try...catch, because they occur before execution even begins.

6. URIError - an error in URI functions

Occurs if the encodeURI(), decodeURI(), encodeURIComponent(), or decodeURIComponent() functions receive invalid data.

javascript
decodeURIComponent("%"); // URIError: URI malformed

7. EvalError - an error from using eval()

Rare in modern implementations. Originally used when eval() was used incorrectly.

javascript
throw new EvalError("Incorrect use of eval()");

8. (Additionally) AggregateError - a set of errors

Occurs when a single operation returns several errors at once, for example with Promise.any().

javascript
Promise.any([ Promise.reject(new Error("Error 1")), Promise.reject(new Error("Error 2")), ]).catch(e => console.log(e.errors));

Output:

javascript
[Error: Error 1, Error: Error 2]

9. Custom errors

You can create your own error classes by extending Error:

javascript
class ValidationError extends Error { constructor(message) { super(message); this.name = "ValidationError"; } } throw new ValidationError("Invalid email");

Output:

javascript
ValidationError: Invalid email

Summary table

Error typeWhen it occursExample
ErrorThe base class for all errorsthrow new Error("Error!")
ReferenceErrorA variable is not definedconsole.log(x)
TypeErrorThe value has the wrong typenull.toString()
RangeErrorA value is outside the allowed rangenew Array(-1)
SyntaxErrorA syntax erroreval("if (true {")
URIErrorAn error encoding/decoding a URIdecodeURI("%")
EvalErrorAn eval() error (rarely used)throw new EvalError()
AggregateErrorSeveral errors at oncePromise.any([...])

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.