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

Message

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

Error type

ReferenceError

๋ฌด์—‡์ด ์ž˜๋ชป ๋˜์—ˆ์„๊นŒ์š”?

๋ณ€์ˆ˜๊ฐ€ ์ดˆ๊ธฐํ™” ๋˜๊ธฐ ์ „์— ์—‘์„ธ์Šค๊ฐ€ ๋˜์–ด๋ฒ„๋ฆฝ๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋Š” let ๋˜๋Š” const ์„ ์–ธ์ด ์ •์˜ ๋˜๊ธฐ ์ „์— ์—‘์„ธ์Šค๋˜๋Š” ๋ชจ๋“  block ๋ฌธ์—์„œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

Examples

์ž˜๋ชป๋œ ๊ฒฝ์šฐ

์ด ๊ฒฝ์šฐ์— ๋ณ€์ˆ˜ "foo"๋Š” let ์„ ์‚ฌ์šฉํ•˜์—ฌ block ๋ฌธ์—์„œ ๋‹ค์‹œ ์„ ์–ธ๋ฉ๋‹ˆ๋‹ค.

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

์˜ฌ๋ฐ”๋ฅธ ๊ฒฝ์šฐ

if ๋ฌธ์—์„œ "foo"๋ฅผ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ์žฌ ์„ ์–ธ์„ ๋ฐœ์ƒ์‹œํ‚ค๋Š” let ์„ ์ œ๊ฑฐํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

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

๊ทธ๋ฐ–์— ๋ณผ ๊ฒƒ