JavaScript Error Object
Error objects represent problems that happen while code runs. They are thrown with throw, caught with try...catch, and carry a message, a name and usually a stack trace.
Description
An Error object describes a runtime error. When something goes wrong, JavaScript throws an instance of Error or one of its subtypes, such as TypeError or RangeError, and you can throw your own with the throw statement. Every error has a name and a message, and browsers add a non-standard stack property showing where it happened.
The optional cause option lets you wrap a lower-level error while keeping the original, which is useful when rethrowing. You can create your own error types by extending Error with a class; set this.name in the constructor so the type shows up in messages.
Built-in subclasses describe specific kinds of problem: TypeError, RangeError, SyntaxError, ReferenceError, URIError, EvalError and AggregateError. Use instanceof in a catch block to handle them differently. Anything can be thrown in JavaScript, including strings, but always throwing Error objects gives you a stack trace and a consistent shape for logging.
Creating
new Error(message)
new Error(message, { cause })Instance properties
| Name | Description |
|---|---|
Error.prototype.cause | The cause property of an Error records the original error that led to it, so wrapped errors keep their underlying reason. |
Error.prototype.message | The message property of an Error holds the human-readable description of what went wrong. |
Error.prototype.name | The name property of an Error gives its type, such as Error, TypeError or a custom name. |
Example
Browser Support
The following information will show you the current browser support for the Error object. Hover over a browser icon to see the version that first introduced support for this object.
Desktop





Tablets & Mobile





