JavaScript Reference ยท JSON, Errors and Reflection
TypeError, RangeError, SyntaxError and other error types
Reference for the built-in JavaScript error types: TypeError, RangeError, ReferenceError, SyntaxError, URIError and EvalError, what triggers each and how to handle them.
Besides plain Error, JavaScript defines a handful of built-in subclasses. Each one signals a category of problem, and error.name tells you which: TypeError, RangeError, ReferenceError, SyntaxError, URIError and EvalError. All of them inherit from Error, so instanceof Error is true for every one, and all accept (message, { cause }).
Knowing the categories helps you read error messages quickly and choose the right type when you throw your own. A fifth family, AggregateError, is covered on its own page.
try { null.length; } catch (error) { console.log(error instanceof TypeError); // true }
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
The error types
| Type | Meaning | Typical causes |
|---|---|---|
Error |
Generic base type. | Your own application errors. |
TypeError |
A value is not of the expected type, or an operation is not allowed on it. | Reading a property of null or undefined, calling something that is not a function, mixing BigInt and Number, assigning to a read-only property in strict mode, invalid arguments such as a non-iterable in for...of. |
RangeError |
A value is a valid type but outside the allowed range. | new Array(-1), toFixed(101), "x".repeat(-1), Number.prototype.toString(1), invalid dates in toISOString(), and too-deep recursion ("Maximum call stack size exceeded" in most engines). |
ReferenceError |
A variable name cannot be resolved. | Using an undeclared variable, or reading a let/const before its declaration runs (the temporal dead zone). |
SyntaxError |
Text is not valid for the parser. | Invalid source code, JSON.parse() of bad JSON, new RegExp("("). |
URIError |
A URI function received malformed input. | decodeURIComponent("%"), encodeURIComponent("\uD800"). |
EvalError |
Historic; engines no longer throw it. | Kept for compatibility only. |
Usage notes
- Messages differ between engines, so branch on
error.nameorinstanceof, never on message text. SyntaxErrorfor source code is thrown at load time, before any of the script runs, so you cannotcatchit in the same script. It is catchable when it comes fromeval(),new Function(),JSON.parse()ornew RegExp().- A
ReferenceErroris about names, aTypeErroris about values.typeof undeclareddoes not throw, butundeclared.xdoes. - Throw the closest type from your own code:
TypeErrorfor wrong argument types,RangeErrorfor numbers out of bounds. Use your ownErrorsubclass for domain problems. - Do not rely on
RangeErrorfor stack overflow detection. Handling it is unreliable; fix the recursion instead. DOMExceptionis separate. Browser APIs throw it with names such as"AbortError"or"NotFoundError", inspected viaerror.name.- Strict mode turns silent failures into
TypeErrors, such as writing to frozen objects or non-writable properties.
Browser support
All of these error types are supported in every browser and runtime.