The Error Object
You'll learn what information JavaScript provides when an error is caught, helping you understand and fix problems in your code.
What's Inside a Caught Error?
When you catch an error with try/catch, the error isn't just a simple message—it's an object packed with helpful information. Think of it like a detailed incident report that tells you not just what went wrong, but where and why.
Every error object comes with three key properties:
1. The message Property
This contains a human-readable description of what went wrong:
try {
throw new Error("Database connection failed");
} catch (error) {
console.log(error.message); // "Database connection failed"
}
2. The name Property
This identifies the type of error. Built-in errors have names like "Error", "TypeError", "ReferenceError", etc.:
try {
let result = null.toUpperCase();
} catch (error) {
console.log(error.name); // "TypeError"
console.log(error.message); // "Cannot read properties of null"
}
3. The stack Property
This is your detective's trail—a multi-line string showing exactly where the error occurred and the sequence of function calls that led to it:
try {
throw new Error("Something broke");
} catch (error) {
console.log(error.stack);
// Shows the error message plus file names, line numbers, and call chain
}
The stack trace is invaluable for debugging, especially in deeply nested code.
Key Takeaway: The error object gives you three essential pieces of information—message (what happened), name (what type), and stack (where and how)—making debugging far more effective than a simple text message ever could.