Suggest an editImprove this articleRefine the answer for “What types of errors exist in JavaScript?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**JavaScript** has several built-in error types that inherit from the base `Error` class: `ReferenceError`, `TypeError`, `RangeError`, `SyntaxError`, `URIError`, `EvalError`, and `AggregateError`. **Key point:** if no specific type fits, plain `Error` can be used, and custom error types are created by extending it.Shown above the full answer for quick recall.Answer (EN)Image## 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 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([...])` |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.