ReferenceError
A variable does not exist error.
Available since: ES3
Syntax
new ReferenceError(message)Parameters
| Parameter | Type | Description |
|---|---|---|
| message | string | Optional. 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 definedUsing a let variable before its declaration:
try {
console.log(total);
let total = 5;
} catch (error) {
console.log(error.name);
}Output:
ReferenceErrorNotes
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
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.