In JavaScript, errors are objects that describe problems in code execution. Recognizing different error types helps in debugging and writing robust code.
| Error Type | Description |
|---|---|
SyntaxError | Occurs when code violates JavaScript syntax rules, e.g., missing brackets or unexpected tokens. |
ReferenceError | Thrown when accessing a variable that is not declared or out of scope. |
TypeError | Raised when a value is not of the expected type or an operation is performed on an incompatible type. |
RangeError | Occurs when a numeric value is outside an allowable range, such as an invalid array length. |
URIError | Thrown when global URI handling functions receive malformed parameters. |
EvalError | Relates to the eval() function misuse; rarely used in modern JavaScript. |

💡 Key Insight
Understanding the error type helps pinpoint the root cause quickly — whether it’s a typo (SyntaxError), missing variable (ReferenceError), or wrong data usage (TypeError).
Below are concise examples demonstrating when each error type might occur:
📌 Deep Dive: Examples of Common Errors
// SyntaxError: missing closing parenthesis
if (true {
console.log('Oops!');
}
// ReferenceError: x is not defined
console.log(x);
// TypeError: undefined is not a function
let num = 5;
num.toUpperCase();
// RangeError: invalid array length
let arr = new Array(-1);
// URIError: malformed URI sequence
decodeURIComponent('%');
// EvalError: deprecated usage (rare)
eval('var a = 1;');
⚠️ Handling Errors
Most errors can be caught using try...catch blocks to prevent script termination and provide graceful recovery.
Errors inherit from the built-in Error object, which holds properties like message and stack useful for debugging.
💡 Summary
SyntaxError: Code structure issues.ReferenceError: Using undeclared variables.TypeError: Invalid operations on data types.RangeError: Values out of allowed range.URIError: Problems with URI functions.EvalError: Issues witheval()(rare).
Quick Knowledge Check
Test what you just learned
Question 1 of 2
Which error is thrown when you try to use a variable that has not been declared?
Question 2 of 2
What error type occurs when you write invalid JavaScript syntax?
Loading results...