0

Why is it logging "Reference Error" in the first case, why didn't it pick it from the global scope like in the second case?

let a = 10;

function test1() {
  console.log(a);
  let a = 100;
}

test1(); // Reference Error

let a = 10;

function test2() {
  console.log(a);
}

test2(); //10
Andreas
  • 21,535
  • 7
  • 47
  • 56
  • You're missing the important part of the error message: _"...can't access lexical declaration 'a' before initialization"_ – Andreas Jul 01 '21 at 08:28
  • @andreas , Can you please elaborate? – user16355750 Jul 01 '21 at 12:40
  • 1
    I've mentioned the relevant part in my comment: _"...can't access lexial declaration..."_. It's also explained in detail in the two dupe targets at the top of your question. – Andreas Jul 01 '21 at 12:46

0 Answers0