ReferenceError

Thrown when code refers to a binding that doesn't exist — the code was valid JavaScript, but the name it looked for wasn't there when it ran.

                new ReferenceError(message)
              

Purpose

A ReferenceError means the engine tried to look up a binding (a variable, function, or class name) and found nothing by that name in scope. Unlike a SyntaxError, the file was valid JavaScript and had already started running — the problem only appears once that specific line executes.

Example

console.log(greting); // typo for "greeting"
Uncaught ReferenceError: greting is not defined

Common causes

  • A typo in a variable or function name.
  • Using a variable before its let/const declaration runs — the “temporal dead zone” — even though the declaration exists later in the same scope.
  • Forgetting to import something that’s defined in another module.
  • Referring to a variable outside the block or function where it was declared with let or const.

Distinguishing it from other errors

A ReferenceError means the name doesn’t exist. Compare with TypeError, which means the name exists and holds a value, but that value doesn’t support the operation attempted on it (for example, calling something that isn’t a function). If nothing ran at all — not even code before the failing line — the problem is a SyntaxError instead.

Runtime notes

ReferenceError is part of ECMAScript, so the error type and the temporal-dead-zone behavior are identical across browsers and Node; only the exact message wording is engine-specific.

Taught in these lessons

Related reference