References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript Error object

Object JavaScript All modern browsers Updated
Quick answer

An Error object represents a runtime error and carries a message, a name and a stack trace. Create one with new Error("message") and raise it with throw. Built-in subtypes like TypeError and RangeError describe specific problems, and you catch errors with try...catch.

Overview

The Error object is how JavaScript represents something going wrong. You create one with new Error("something failed") and throw it to signal the problem. Every error carries useful properties: message (the text you passed), name (the error type, like "Error" or "TypeError"), and stack (a trace of where it happened, invaluable for debugging). That's why you should always throw an Error object rather than a bare string.

JavaScript provides several built-in error types, all subclasses of Error, that the language itself throws and you can throw too: TypeError (a value of the wrong type), RangeError (a number out of range), SyntaxError (invalid code, e.g. from JSON.parse()), and ReferenceError (an undefined variable). Using the right type makes errors self-describing.

You handle errors with try...catch, and inside catch you can branch on the type using instanceof: if (err instanceof TypeError) { ... }. For your own domain-specific errors, define a custom error by extending Error with a classclass ValidationError extends Error {} — which lets callers catch your specific type. Modern errors also support a cause option (new Error("msg", { cause: original })) to chain the underlying error.

Syntax

throw new Error("Something went wrong");

// built-in types
throw new TypeError("Expected a number");
throw new RangeError("Out of range");

// custom error
class ValidationError extends Error {
  constructor(msg) { super(msg); this.name = "ValidationError"; }
}

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  function parse(text) {
    try {
      return JSON.parse(text);
    } catch (err) {
      return err.name + ': ' + err.message;
    }
  }

  document.getElementById('out').textContent =
    parse('{"ok":true}') + '\n' +
    parse('bad json'); // [object Object] / SyntaxError: ...
</script>

Best practices

  • Always throw an Error object (not a string) so you get a message and stack.
  • Use the specific built-in type that fits — TypeError, RangeError, etc.
  • In catch, branch on type with instanceof.
  • Define custom errors by extending Error with a class for domain-specific failures.

Frequently asked questions

How do I create an error in JavaScript?
Use new Error("message"), then raise it with throw. It carries a message and a stack trace.
What are the built-in error types?
TypeError, RangeError, SyntaxError, ReferenceError and a few more — all subclasses of Error for specific problems.
How do I create a custom error?
Extend Error with a class: class MyError extends Error { constructor(m){ super(m); this.name = "MyError"; } }.
How do I check the type of an error?
Use instanceof in the catch block, e.g. if (err instanceof TypeError).