Errors
Errors in JavaScript are issues that occur during runtime, represented by the Error
object. This object provides information about the error type and message and can be used to throw exceptions or handle them in a try-catch
block.
When JavaScript throws an error, it throws an error object that consists of a name
and a message
property. The name
is the general type of the error, and the message
is a human-readable description of the specific error that happened.
Thrown errors are caught by the next outer catch
block of a try...catch...finally
statement. They can also be thrown intentionally by the throw
statement.
The Error Object
The error object holds information about the exception that was thrown in its three properties:
name
Sets or returns an error name. (Type of Error)message
Sets or returns an error message. (Description of specific instance.)stack
Returns a string representing the point in the code where the error was instantiated.
The following types of error can be returned by the name
property:
- “EvalError” An error has occurred in the eval() function (Note: Depreciated in newer versions of JavaScript)
- “RangeError” A number “out of range” has occurred
- “ReferenceError” An illegal reference has occurred
- “SyntaxError” A syntax error has occurred
- “TypeError” A type error has occurred
- “URIError” An error in encodeURI() has occurred
These are some example messages for various types of errors:
- RangeError
- invalid array length
- invalid date
let arr = new Array(-1); // RangeError: Invalid array length
- ReferenceError
- “x” is not defined
- assignment to undeclared variable “x”
console.log(x); // ReferenceError: x is not defined
- SyntaxError
- “x” is a reserved identifier
- a declaration in the head of a for-of loop can’t have an initializer
// SyntaxError: 'for-of' loop variable can't be initializedtry {for (let i = 0 of [1, 2, 3]) {console.log(i);}} catch (err) {console.log(err.name); // "SyntaxError"console.log(err.message); // "'for-of' loop variable can't have an initializer"}
- TypeError
- “x” is not a function
- “x” is read-only
let num = 42;num.toUpperCase(); // TypeError: num.toUpperCase is not a function
- URIError
- malformed URI sequence
// This is an invalid percent encoding (notice '%E0%' is incomplete).try {let badURI = decodeURIComponent('%E0%');} catch (err) {console.log(err.name); // "URIError"console.log(err.message); // "malformed URI sequence"}
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Skill path
Create a Back-End App with JavaScript
Learn how to build back-end web APIs using Express.js, Node.js, SQL, and a Node.js-SQLite database library.Includes 8 CoursesWith CertificateBeginner Friendly30 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours