1
var a=10;
{
 console.log(a);
 let a =20 ;
}

ReferenceError: a is not defined

Why it is giving the reference error even a is declared using var which should have its scope inside the block too ?

  • 3
    Because you've removed it from scope by declaring `a` again inside the function. It can't have both. It doesn't matter if it hasn't been declared yet within the function when you log it. – Brian Thompson May 29 '20 at 16:47
  • 2
    Inside the block, above `let a = 20` line, it's in the *Temporal Dead Zone*. Check the duplicates for more details – adiga May 29 '20 at 16:49
  • 1
    Read about [hoisting here](https://developer.mozilla.org/en-US/docs/Glossary/Hoisting). Someone can correct me if needed, but I think this is what causes this behavior. – Brian Thompson May 29 '20 at 16:50

0 Answers0