Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

ReferenceError

BaselineWidely available *

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 newReferenceError 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. ForReferenceError instances, the initial value is theReferenceError constructor.

ReferenceError.prototype.name

Represents the name for the type of error. ForReferenceError.prototype.name, the initial value is"ReferenceError".

Instance methods

Inherits instance methods from its parentError.

Examples

Catching a ReferenceError

js
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

js
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

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp