Skip to content

Built-in error types — TypeError, RangeError, SyntaxError and when each appears

Level: 101 · for anyone who has read an error and wondered why it has that type

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.

One line: The engine picks the type: TypeError for using a value the wrong way, ReferenceError for an unknown name, RangeError for a number out of range, SyntaxError for text it cannot parse — and EvalError only when the host forbids eval altogether.

Keywords: TypeError, RangeError, SyntaxError, ReferenceError, URIError, EvalError

What the finished page will answer

  • Which error type do null.x, undefined(), assigning to a const and 1n + 1 throw?
  • What throws RangeError — new Array(-1), (1).toFixed(101), recursion without end — and what do the messages say?
  • Why is a SyntaxError in your own file thrown before its first line runs, while JSON.parse("{") throws at run time and can be caught?
  • When is ReferenceError thrown for a name that is declared?
  • Which built-in error does the specification never throw, and what makes V8 throw it anyway?
  • Which parts of an error can a program rely on — the constructor and name — and which differ between engines, such as the message text?

Examples it will need

  • error_types_by_mistake_js.js — one line per mistake: the code, the error type it throws and the message
  • error_types_syntax_before_run_sh.sh — a file whose first line logs and whose second is a syntax error: nothing printed, the SyntaxError and the exit status
  • error_types_evalerror_sh.sh — eval under --disallow-code-generation-from-strings throwing EvalError

See also

Sources to start from