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.
throw new Error("Something went wrong!");Properties:
name- the error's name ("Error")message- the descriptionstack- 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.
console.log(x); // ReferenceError: x is not definedIt 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.
let n = null;
console.log(n.toString()); // TypeError: Cannot read properties of nulllet func = 123;
func(); // TypeError: func is not a function4. RangeError - out of the allowed range
Occurs when a passed value is outside the allowed range.
new Array(-5); // RangeError: Invalid array length(123.456).toFixed(200); // RangeError: toFixed() digits argument must be between 0 and 1005. SyntaxError - a syntax error
An error caused by violating JavaScript syntax. The engine cannot "parse" the code, so the program does not even start.
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.
decodeURIComponent("%");
// URIError: URI malformed7. EvalError - an error from using eval()
Rare in modern implementations.
Originally used when eval() was used incorrectly.
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().
Promise.any([
Promise.reject(new Error("Error 1")),
Promise.reject(new Error("Error 2")),
]).catch(e => console.log(e.errors));Output:
[Error: Error 1, Error: Error 2]9. Custom errors
You can create your own error classes by extending Error:
class ValidationError extends Error {
constructor(message) {
super(message);
this.name = "ValidationError";
}
}
throw new ValidationError("Invalid email");Output:
ValidationError: Invalid emailSummary table
| Error type | When it occurs | Example |
|---|---|---|
| Error | The base class for all errors | throw new Error("Error!") |
| ReferenceError | A variable is not defined | console.log(x) |
| TypeError | The value has the wrong type | null.toString() |
| RangeError | A value is outside the allowed range | new Array(-1) |
| SyntaxError | A syntax error | eval("if (true {") |
| URIError | An error encoding/decoding a URI | decodeURI("%") |
| EvalError | An eval() error (rarely used) | throw new EvalError() |
| AggregateError | Several errors at once | Promise.any([...]) |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.