Built-in Error Types
You'll learn about JavaScript's native error categories and when each one occurs.
What Are Built-in Error Types?
JavaScript doesn't just throw generic "Error" objects—it provides specific error types that describe what went wrong. Think of them like medical diagnoses: instead of just saying "you're sick," a doctor specifies "you have the flu" or "you have a broken bone." Each error type helps you understand the nature of the problem.
The Main Built-in Error Types
TypeError
Occurs when a value isn't the expected type, like trying to call something that isn't a function or access properties on null or undefined.
ReferenceError
Thrown when you try to use a variable that doesn't exist or hasn't been declared.
RangeError
Happens when a numeric value is outside its allowed range, like creating an array with a negative length or calling a recursive function too many times (stack overflow).
SyntaxError
Triggered when JavaScript code violates language rules. This usually happens during parsing, before code execution begins.
Why Different Types Matter
When you use try/catch, you can check which error type occurred using instanceof. This lets you handle different problems differently—maybe retry on network errors but log and stop on syntax errors.
These built-in types all inherit from the base Error constructor you learned about earlier, so they share properties like message and stack, but their specific type gives you valuable context about what failed.
Key Takeaway: JavaScript provides specific error types (TypeError, ReferenceError, RangeError, SyntaxError) that categorize runtime problems, making it easier to diagnose and handle different failure scenarios appropriately.