JavaScript has several built-in error object types that are thrown when runtime errors occur.
-
SyntaxError
: An error in the code's syntax. Usually caught during parsing. Example:let x = 5y;
-
ReferenceError
: Thrown when trying to access a variable that has not been declared or is not in scope. Example:console.log(undeclaredVariable);
-
TypeError
: Thrown when an operation cannot be performed, typically when a value is not of the expected type. Examples:- Calling something that is not a function:
let x = 10; x();
- Accessing properties of
null
orundefined
:let obj = null; obj.property;
- Using an operator on an incompatible type:
(10).split('');
- Calling something that is not a function:
-
RangeError
: Thrown when a numeric variable or parameter is outside of its valid range. Examples:new Array(-1)
(Array length cannot be negative)(10).toFixed(101)
(Number of digits fortoFixed
is out of range 0-100)
-
URIError
: Thrown when a global URI handling function (likeencodeURIComponent()
,decodeURI()
) was used in a way that is incompatible with its definition. -
Error
(Generic): A base type for all errors. Sometimes generic errors are thrown, or custom errors inherit from this.
Understanding these common error types helps you quickly identify the nature of a problem when it appears in the console.