The built-in error types
An exception is an error thrown at runtime that unwinds the call stack until something catches it — or nothing does, and the program (or the page) reports "Uncaught". JavaScript has one base Error and a handful of subclasses. The type tells you the category of mistake; the message tells you the specifics. Recognise them and most bugs diagnose themselves.
You should see
ReferenceError: undefinedVariable is not defined
instanceof Error: true | constructor: ReferenceError
TypeError: Cannot read properties of null (reading 'property')
instanceof Error: true | constructor: TypeError
RangeError: toFixed() digits argument must be between 0 and 100
instanceof Error: true | constructor: RangeError
SyntaxError: Expected property name or '}' in JSON at position 1 (line 1 column 2)
instanceof Error: true | constructor: SyntaxError
URIError: URI malformed
instanceof Error: true | constructor: URIError
Error: custom
instanceof Error: true | constructor: Error| Type | Means | Typical cause |
|---|---|---|
ReferenceError | A name does not exist | Typo, wrong scope, using a let before its line |
TypeError | A value is the wrong type for what you did | Reading a property of undefined/null, calling a non-function, reassigning a const |
SyntaxError | Code (or JSON) could not be parsed | Missing bracket, import in a non-module, bad JSON |
RangeError | A number is out of range | Infinite recursion (call stack), new Array(-1), toFixed(200) |
URIError | Bad percent-encoding | decodeURIComponent("%") |
AggregateError | Several errors at once | Promise.any when everything rejects |
