Skip to content

Error

In one line

An Error is an object thrown when something goes wrong, containing a message and stack trace.

In simple words

An Error is a built-in object that represents a runtime problem. JavaScript throws errors when code fails — dividing by zero gives Infinity (not an error), but calling an undefined variable throws a ReferenceError. You can also throw errors yourself with throw new Error('message').

JavaScript has several error types: Error (base), TypeError (wrong type), ReferenceError (undefined variable), SyntaxError (bad syntax), RangeError (value out of range), and URIError. Each has a name and message property.

Errors are caught with try/catch blocks. In async code, use try/catch inside async functions or .catch() on Promises. Uncaught errors crash the program (or produce unhandled promise rejections).

Example

javascript
try {
  throw new Error("Something went wrong");
} catch (err) {
  console.log(err.name);    // "Error"
  console.log(err.message); // "Something went wrong"
}

Errors are thrown and caught with try/catch. They have name and message properties.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary