name
The error type name, such as TypeError.
Error objects carry a name, message, stack and sometimes a cause. They are the data structure of failure.
const error = new TypeError("Expected a string");
console.log(error.name);
console.log(error.message);
Errors & Debugging
The base Error type has a message. Runtime errors such as TypeError, RangeError and SyntaxError use specialized names.
The stack trace points to where the error was created or thrown. Stack output differs by browser and runtime, but it is essential for debugging.
Modern Error objects can include a cause, which lets you wrap lower-level failures without losing the original error.
The error type name, such as TypeError.
Human-readable explanation.
Trace of where the error happened.
Original error when wrapping another error.
Examples
function setScore(score) {
if (!Number.isFinite(score)) {
throw new TypeError("score must be a number");
}
if (score < 0 || score > 100) {
throw new RangeError("score must be 0-100");
}
}
throw "Something failed"; // This loses normal Error object behavior.
Code patterns
These examples focus on the debugging patterns you will reuse: safe parsing, throwing, custom error types, console inspection, breakpoints and async failure handling.
Base error with a message.
const error = new Error("Something failed");
Use for wrong value type.
throw new TypeError("title must be a string");
Use for values outside an allowed range.
throw new RangeError("score must be 0-100");
Wrap a lower-level failure.
throw new Error("Could not load settings", {
cause: originalError
});
Rules that matter
Debugging starts before something breaks. Clear error types, helpful messages and visible failure states make the code easier to repair.
They carry message, name and stack.
TypeError and RangeError communicate intent.
Say what was wrong, not just that it failed.
Keep the original error connected.
Internal stacks belong in logs, not public messages.
The same message can happen in multiple operations.
Production thinking
Error objects are the debugging trail. Better error data means faster repair.
User-facing error text should be clear and safe, while technical detail goes to logs.
Production error reporting should preserve type, message, stack and context.
Server-rendered or build-time errors should fail loudly so broken content is not published silently.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
A JavaScript feature is not production-ready until you know how it fails and how you will inspect that failure.