This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

ReferenceError

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015년 7월.

* Some parts of this feature may have varying levels of support.

ReferenceError 객체는 현재 범위에서 존재하지 않거나 초기화되지 않은 변수를 참조했을 때 발생하는 에러를 나타냅니다.

ReferenceError직렬화 가능한 객체이기 때문에, structuredClone() 혹은 Workers 사이에서 postMessage()를 사용하여 복사할 수 있습니다.

생성자

ReferenceError()

새로운 ReferenceError 객체를 생성합니다.

인스턴스 속성

ReferenceError.prototype.message

오류 ���시지. Error에서 상속되었습니다.

ReferenceError.prototype.name

오류 이름. Error에서 상속되었습니다.

ReferenceError.prototype.cause

오류 원인. Error에서 상속되었습니다.

ReferenceError.prototype.fileName

오류가 발생한 파일 경로. Error에서 상속되었습니다.

ReferenceError.prototype.lineNumber

오류가 발생한 곳의 파일 내 줄 수. Error에서 상속되었습니다.

ReferenceError.prototype.columnNumber

오류가 발생한 곳의 줄에서 열 위치. Error에서 상속되었습니다..

ReferenceError.prototype.stack

스택 추적. Error에서 상속되었습니다.

예제

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.fileName); // "Scratchpad/1"
  console.log(e.lineNumber); // 2
  console.log(e.columnNumber); // 6
  console.log(e.stack); // "@Scratchpad/2:2:7\n"
}

ReferenceError 생성하기

js
try {
  throw new ReferenceError("Hello", "someFile.js", 10);
} catch (e) {
  console.log(e instanceof ReferenceError); // true
  console.log(e.message); // "Hello"
  console.log(e.name); // "ReferenceError"
  console.log(e.fileName); // "someFile.js"
  console.log(e.lineNumber); // 10
  console.log(e.columnNumber); // 0
  console.log(e.stack); // "@Scratchpad/2:2:9\n"
}

명세

Specification
ECMAScript® 2027 Language Specification
# sec-native-error-types-used-in-this-standard-referenceerror

브라우저 호환성

같이 보기