This is from O'Reilly's Learning JavaScript Design Patterns (by Addy Osmani). The below is what author says about var
, let
and const
.
The difference between
var
,const
andlet
is thatvar
is a block-scoped variable,const
is a block-scoped constant andlet
is a block-scoped variable that is only accessible within the block it is declared in. Often, you will want to use let but if you want to avoid variable assignment, you can useconst
instead. Ultimately, use the convention already in the codebase you're using.
I don't understand how can he say var
is block scoped? If my understanding is right, block is the pair of parentheses. And the scope of var should be the current execution.