Error Types
JavaScript has several built-in error types.
You do not need to memorize every error type immediately, but recognizing the common ones helps you debug faster.
Every error object usually has:
error.name
error.message
error.stackExample:
try {
missingVariable;
} catch (error) {
console.log(error.name); // ReferenceError
console.log(error.message);
}Error
Error is the general error type.
throw new Error("Something went wrong");Use Error when you do not need a more specific built-in error type.
function createUser(name) {
if (!name) {
throw new Error("Name is required");
}
return { name };
}ReferenceError
A ReferenceError happens when code tries to use a variable that does not exist in scope.
console.log(username); // ReferenceErrorCommon causes:
- misspelled variable names
- using a variable before declaration
- expecting a variable from another scope
Example:
const userName = "Alice";
console.log(username); // ReferenceError: username is not definedJavaScript is case-sensitive.
userName and username are different names.
TypeError
A TypeError happens when a value is not the type you expected for an operation.
const user = null;
console.log(user.name); // TypeErrorYou tried to read a property from null.
Another example:
const value = 42;
value.toUpperCase(); // TypeErrorNumbers do not have toUpperCase().
Common causes:
- calling something that is not a function
- reading properties from
nullorundefined - using a method on the wrong data type
SyntaxError
A SyntaxError happens when JavaScript code or JSON text has invalid syntax.
if (true {
console.log("missing parenthesis");
}This code cannot be parsed.
JSON.parse() can also throw SyntaxError.
JSON.parse("{ bad json }"); // SyntaxErrorSyntax errors often prevent code from running at all.
RangeError
A RangeError happens when a value is outside an allowed range.
const number = 123.456;
number.toFixed(200); // RangeErrorAnother common example:
function recurse() {
recurse();
}
recurse(); // RangeError: maximum call stack size exceededThis happens because the recursion never stops.
URIError
A URIError happens when URI encoding or decoding is invalid.
decodeURIComponent("%"); // URIErrorThis is less common for beginners, but you may see it when working with URLs.
AggregateError
AggregateError represents multiple errors grouped together.
You may see it with promise-related APIs such as Promise.any().
Promise.any([
Promise.reject(new Error("A failed")),
Promise.reject(new Error("B failed")),
]).catch((error) => {
console.log(error instanceof AggregateError); // true
});You will learn promises later.
For now, just recognize that some APIs can group multiple errors.
Checking Error Types
You can use instanceof to check an error type.
try {
JSON.parse("{ bad json }");
} catch (error) {
if (error instanceof SyntaxError) {
console.log("Invalid JSON syntax");
} else {
throw error;
}
}This is useful when you want to handle only specific errors.
Do not catch every error the same way if the response should be different.
Reading Stack Traces
The stack trace shows where an error happened.
try {
throw new Error("Failed");
} catch (error) {
console.log(error.stack);
}A stack trace usually includes:
- the error message
- the function where it happened
- the chain of function calls
- file names and line numbers
Stack traces are mainly for debugging.
Do not show raw stack traces to end users.
Best Practices
Read error.name and error.message when debugging.
Use instanceof when you need different handling for different error types.
Handle expected errors specifically.
Rethrow unexpected errors.
Do not show internal stack traces to users.
Common Mistakes
Mistake 1: Treating All Errors the Same
catch (error) {
return null;
}This might hide serious bugs.
Handle what you expect.
Rethrow what you do not understand.
Mistake 2: Confusing ReferenceError and TypeError
missingVariable; // ReferenceErrornull.name; // TypeErrorReference means the name does not exist.
Type means the value exists but cannot be used that way.
Mistake 3: Ignoring the Stack Trace
The stack trace often points directly to where the problem started.
Use it while debugging.
Quick Check
What kind of error is this?
console.log(total);If total was never declared, it is a ReferenceError.
What kind of error is this?
const user = undefined;
console.log(user.name);It is a TypeError.
Summary
JavaScript has several built-in error types.
Erroris the general error type.ReferenceErrormeans a variable name cannot be found.TypeErrormeans a value is being used in an invalid way.SyntaxErrormeans code or JSON syntax is invalid.RangeErrormeans a value is outside an allowed range.- Use
instanceofto handle specific error types. - Stack traces help with debugging.