DEV Community

Discussion on: Lexical Variable Scoping with Javascript

Collapse
 
asleepysamurai profile image
Balaganesh Damodaran

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.