ReferenceError: can't access lexical declaration`X' before initialization

이 문서는 아직 자원 봉사자들이 한국어로 번역하지 않았습니다. 참여해서 번역을 마치도록 도와 주세요!
English (US)의 문서도 읽어보세요.

Message

ReferenceError: can't access lexical declaration `X' before initialization (Firefox)
ReferenceError: 'x' is not defined (Chrome)

Error type

ReferenceError

What went wrong?

A lexical variable was accessed before it was initialized. This happens within any block statement, when let or const declarations are accessed before they are defined.

Examples

Invalid cases

In this case, the variable "foo" is redeclared in the block statement using let.

function test() {
  let foo = 33;
  if (true) {
    let foo = (foo + 55); 
    // ReferenceError: can't access lexical 
    // declaration `foo' before initialization
  }
}
test();

Valid cases

To change "foo" inside the if statement, you need to remove the let that causes the redeclaration.

function test(){
   let foo = 33;
   if (true) {
      foo = (foo + 55);
   }
}
test();

See also

문서 태그 및 공헌자

 이 페이지의 공헌자: fscholz, johnjago, jonathanKingston
 최종 변경: fscholz,