ReferenceError
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
TheReferenceError object represents an error when a variable that doesn't exist (or hasn't yet been initialized) in the current scope is referenced.
ReferenceError is aserializable object, so it can be cloned withstructuredClone() or copied betweenWorkers usingpostMessage().
ReferenceError is a subclass ofError.
In this article
Constructor
ReferenceError()Creates a new
ReferenceErrorobject.
Instance properties
Also inherits instance properties from its parentError.
These properties are defined onReferenceError.prototype and shared by allReferenceError instances.
ReferenceError.prototype.constructorThe constructor function that created the instance object. For
ReferenceErrorinstances, the initial value is theReferenceErrorconstructor.ReferenceError.prototype.nameRepresents the name for the type of error. For
ReferenceError.prototype.name, the initial value is"ReferenceError".
Instance methods
Inherits instance methods from its parentError.
Examples
>Catching a ReferenceError
try { let a = undefinedVariable;} catch (e) { console.log(e instanceof ReferenceError); // true console.log(e.message); // "undefinedVariable is not defined" console.log(e.name); // "ReferenceError" console.log(e.stack); // Stack of the error}Creating a ReferenceError
try { throw new ReferenceError("Hello");} catch (e) { console.log(e instanceof ReferenceError); // true console.log(e.message); // "Hello" console.log(e.name); // "ReferenceError" console.log(e.stack); // Stack of the error}Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-native-error-types-used-in-this-standard-referenceerror> |