Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

ReferenceError

A variable does not exist error.

Available since: ES3

Syntax

new ReferenceError(message)

Parameters

ParameterTypeDescription
messagestringOptional. A description of the error.

Return value

ReferenceErrorAn error object.

Examples

A typo in a variable name:

const userName = 'Anna';
 
try {
  console.log(username);
} catch (error) {
  console.log(String(error));
}

Output:

ReferenceError: username is not defined

Using a let variable before its declaration:

try {
  console.log(total);
  let total = 5;
} catch (error) {
  console.log(error.name);
}

Output:

ReferenceError

Notes

Variable names are case-sensitive: userName and username are different variables. To safely check whether a global variable exists use typeof name === 'undefined'.

See also

  • TypeError — A wrong type error — for example, calling a method on undefined.
  • let — Declares a variable whose value can change.
  • typeof — Returns a string with the type of a value.

In the tutorial

  • Errors and try...catch — Error handling in JavaScript: try, catch and finally blocks, the error object, the throw statement and built-in error types.

Thanks for reading!

Donate to the site — we work hard for you.