+ 2

Can somebody explain this output?

I know the variable let is scoped but in this case it is still within the scope/brackets of the function. So why is the let variable within the boolean of the function considered different from the first one as it is still within the scope of the same function? function letTest() { let x = 1; if (true) { let x = 2; // different variable console.log(x); // 2 } console.log(x); // 1 }

5th Mar 2021, 9:28 PM
SuperKitsune94
SuperKitsune94 - avatar
2 Answers
+ 4
there are two (three) types of variables scope in JS: var => function/global scoped let, const => block scoped (arguments => function scoped) arguments scope wrap functions scope wich wrap blocks scopes...
5th Mar 2021, 10:35 PM
visph
visph - avatar