JavaScript Error Objects
The various Error objects you will find in JavaScript
React Masterclass
Launching on November 4th
JavaScript gives us 7 error objects, which are raised in a try/catch expression depending on the error type:
ErrorEvalErrorRangeErrorReferenceErrorSyntaxErrorTypeErrorURIError
Let’s analyze each one of those.
Error
This is the generic error, and it’s the one all the other error objects inherit from. You will never see an instance of Error directly, but rather JavaScript fires one of the other errors listed above, which inherit from Error.
It contains 2 properties:
message: the error description, a human readable message that should explain what error happenedname: the type of error occurred (assumes the value of the specific error object name, for example,TypeErrororSyntaxError)
and provides just one method, toString(), which is responsible for generating a meaningful string from the error, which can be used to print it to screen.
EvalError
This error is defined in modern JavaScript but never actually thrown by JavaScript, and remains for compatibility purposes. It was defined in ECMAScript 3 but it’s not present in the standard since ECMAScript 5.1.
It was used to indicate that the global function eval() was used incorrectly, in a way incompatible with its definition.
RangeError
A RangeError will fire when a numeric value is not in its range of allowed values.
The simplest example is when you set an array length to a negative value:
[].length = -1 //RangeError: Invalid array length
or when you set it to a number higher than 4294967295
[].length = 4294967295 //4294967295
[].length = 4294967296 //RangeError: Invalid array length
(this magic number is specified in the JavaScript spec as the maximum range of a 32-bit unsigned integer, equivalent to Math.pow(2, 32) - 1)
Here are the most common range errors you can spot in the wild:
RangeError: argument is not a valid code pointRangeError: invalid array lengthRangeError: invalid dateRangeError: precision is out of rangeRangeError: radix must be an integerRangeError: repeat count must be less than infinityRangeError: repeat count must be non-negative
ReferenceError
A ReferenceError indicates that an invalid reference value has been detected: a JavaScript program is trying to read a variable that does not exist.
dog //ReferenceError: dog is not defined
dog = 2 //ReferenceError: dog is not defined
Be aware that the above statement will create a dog variable on the global object if not ran in strict mode.
Here are the most common reference errors you can spot in the wild:
ReferenceError: "x" is not definedReferenceError: assignment to undeclared variable "x"ReferenceError: can't access lexical declaration 'X' before initializationReferenceError: deprecated caller or arguments usageReferenceError: invalid assignment left-hand sideReferenceError: reference to undefined property "x"
SyntaxError
A SyntaxError is raised when a syntax error is found in a program.
Here are some examples of code that generate a syntax error.
A function statement without name:
function() {
return 'Hi!'
}
//SyntaxError: function statement requires a name
Missing comma after an object property definition:
const dog = {
name: 'Roger'
age: 5
}
//SyntaxError: missing } after property list
Here are the most common syntax errors you can spot in the wild:
SyntaxError: "0"-prefixed octal literals and octal escape seq. are deprecatedSyntaxError: "use strict" not allowed in function with non-simple parametersSyntaxError: "x" is a reserved identifierSyntaxError: JSON.parse: bad parsingSyntaxError: Malformed formal parameterSyntaxError: Unexpected tokenSyntaxError: Using //@ to indicate sourceURL pragmas is deprecated. Use //# insteadSyntaxError: a declaration in the head of a for-of loop can't have an initializerSyntaxError: applying the 'delete' operator to an unqualified name is deprecatedSyntaxError: for-in loop head declarations may not have initializersSyntaxError: function statement requires a nameSyntaxError: identifier starts immediately after numeric literalSyntaxError: illegal characterSyntaxError: invalid regular expression flag "x"SyntaxError: missing ) after argument listSyntaxError: missing ) after conditionSyntaxError: missing : after property idSyntaxError: missing ; before statementSyntaxError: missing = in const declarationSyntaxError: missing \] after element listSyntaxError: missing formal parameterSyntaxError: missing name after . operatorSyntaxError: missing variable nameSyntaxError: missing } after function bodySyntaxError: missing } after property listSyntaxError: redeclaration of formal parameter "x"SyntaxError: return not in functionSyntaxError: test for equality (==) mistyped as assignment (=)?SyntaxError: unterminated string literal
TypeError
A TypeError happens when a value has a type that’s different than the one expected.
The simplest example is trying to invoke a number:
1() //TypeError: 1 is not a function
Here are the most common type errors you can spot in the wild:
TypeError: "x" has no propertiesTypeError: "x" is (not) "y"TypeError: "x" is not a constructorTypeError: "x" is not a functionTypeError: "x" is not a non-null objectTypeError: "x" is read-onlyTypeError: 'x' is not iterableTypeError: More arguments neededTypeError: Reduce of empty array with no initial valueTypeError: can't access dead objectTypeError: can't access property "x" of "y"TypeError: can't define property "x": "obj" is not extensibleTypeError: can't delete non-configurable array elementTypeError: can't redefine non-configurable property "x"TypeError: cannot use 'in' operator to search for 'x' in 'y'TypeError: cyclic object valueTypeError: invalid 'instanceof' operand 'x'TypeError: invalid Array.prototype.sort argumentTypeError: invalid argumentsTypeError: invalid assignment to const "x"TypeError: property "x" is non-configurable and can't be deletedTypeError: setting getter-only property "x"TypeError: variable "x" redeclares argument
URIError
This error is raised when calling one of the global functions that work with URIs:
decodeURI()decodeURIComponent()encodeURI()encodeURIComponent()
and passing an invalid URI.
I wrote 20 books to help you become a better developer:
- JavaScript Handbook
- TypeScript Handbook
- CSS Handbook
- Node.js Handbook
- Astro Handbook
- HTML Handbook
- Next.js Pages Router Handbook
- Alpine.js Handbook
- HTMX Handbook
- React Handbook
- SQL Handbook
- Git Cheat Sheet
- Laravel Handbook
- Express Handbook
- Swift Handbook
- Go Handbook
- PHP Handbook
- Python Handbook
- Linux/Mac CLI Commands Handbook
- C Handbook