Apart from explaining lexical scope, this is also a great example to show why you must never use var. var and let have different scoping rules, and as such let is always bound to the block scope, which includes conditionals and loop blocks also.
For a newbie Javascript programmer, it is much easier to understand that a variable declared with let always references the variable in the closest block scope where it is defined in.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Apart from explaining lexical scope, this is also a great example to show why you must never use
var
.var
andlet
have different scoping rules, and as suchlet
is always bound to the block scope, which includes conditionals and loop blocks also.For a newbie Javascript programmer, it is much easier to understand that a variable declared with
let
always references the variable in the closest block scope where it is defined in.