0

I am learning JavaScript at the moment and please can someone explain to me why the following function would give the value of 9 and 10, as the let a = 10 is outside of the function block and I thought with let variables as they are block-scoped there are not accessible outside the {block}

let a = 10;
function f() {
  let b = 9;
  console.log(b);
  console.log(a);
}
f();
LiBee
  • 5
  • 4
  • 3
    Variables declared in an inner block are not visible outside. Variables declared in an outer block are always visible to everything in that outer block, including inner blocks. – CertainPerformance Jan 13 '22 at 22:23
  • @CertainPerformance thank you - so essentially as a = 10, that variable is visible to the inner block which is why it prints 9 (i.e. console.log(a)) – LiBee Jan 13 '22 at 22:28

0 Answers0