0

The following code got this error: ReferenceError: aa is not defined. Why not print '2' ?

var aa = 2;

function run() {
  console.log(aa);
  let aa = 4
}
run()
Satpal
  • 132,252
  • 13
  • 159
  • 168
jiayu lou
  • 47
  • 1
  • 1
    You might need this https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – Zyigh Jul 25 '18 at 08:54
  • 1
    See the linked question's answers. Basically: The `let aa` inside `run` shadows the `aa` outside `run`, but cannot be accessed until the `let aa` is reached within `run`. The area from the beginning of the scope to the `let aa` is called the *temporal dead zone* during which the identifier is reserved and cannot be used, but isn't yet declared. – T.J. Crowder Jul 25 '18 at 08:56
  • 1
    Why downvote this question? This is a relatively-obscure aspect of `let` which is tricky to search for. – T.J. Crowder Jul 25 '18 at 08:56

0 Answers0