JavaScript ReferenceError Object
A ReferenceError is thrown when code uses a variable that doesn't exist, or a let or const variable before it has been declared.
Description
A ReferenceError is thrown when you refer to a variable that doesn't exist, usually because of a typo or a missing declaration ("x is not defined"). It is also thrown when a let, const or class binding is used before its declaration runs, in what is called the temporal dead zone.
To check whether a global variable exists without risking this error, use typeof name !== 'undefined', since typeof doesn't throw for undeclared names.
Typos in variable names are the most common cause, and editors with TypeScript or a linter flag them before the code runs. In non-strict scripts, assigning to an undeclared variable silently creates a global instead of throwing, which hides mistakes; strict mode, modules and classes throw a ReferenceError instead.
Creating
new ReferenceError(message)Example
Browser Support
The following information will show you the current browser support for the ReferenceError object. Hover over a browser icon to see the version that first introduced support for this object.
Desktop





Tablets & Mobile





