ReferenceError
BaselineWidely 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
.
Constructor
ReferenceError()
Creates a new
ReferenceError
object.
Instance properties
Also inherits instance properties from its parentError
.
These properties are defined onReferenceError.prototype
and shared by allReferenceError
instances.
ReferenceError.prototype.constructor
The constructor function that created the instance object. For
ReferenceError
instances, the initial value is theReferenceError
constructor.ReferenceError.prototype.name
Represents 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 |