JavaScript · Error Handling · Junior · Best Practice
What built-in Error types should you know, and when do you create custom errors?
Short Interview Answer
Know `Error`, `TypeError`, `ReferenceError`, `RangeError`, `SyntaxError`; create custom subclasses for domain-specific failures with stable codes.
Detailed Explanation
Built-ins communicate programmer mistakes vs invalid values. Custom errors (`class ValidationError extends Error`) enable `instanceof` checks and structured fields (`code`, `details`). Set `name` and use `super(message)`; in older targets, fix prototype chain. Prefer error causes (`{ cause }`) to wrap lower-level failures without losing stacks. Don't over-hierarchize — a few meaningful types beat dozens.
Example
class HttpError extends Error {
constructor(status, message) {
super(message);
this.name = 'HttpError';
this.status = status;
}
}Interview Tip
Show a small custom Error class.
Common Mistake
Throwing strings instead of Error objects (loses stack).