Variables and scoping in ECMAScript 6
http://2ality.com/2015/02/es6-scoping.html This blog post examines how variables and scoping are handled in ECMAScript 6 [1] . Block scoping via let and const Both let and const create variables that are block-scoped – they only exist within the innermost block that surrounds them. The following code demonstrates that the let -declared variable tmp only exists inside the then-block of the if statement: function func ( ) { if ( true ) { let tmp = 123 ; } console .log(tmp); // ReferenceError: tmp is not defined } In contrast, var -declared variables are function-scoped: function func ( ) { if ( true ) { var tmp = 123 ; } console .log(tmp); // 123 } Block scoping means that you can shadow variables within a function: function func ( ) { let foo = 5 ; if (···) { let foo = 10 ; // shadows outer `foo` cons...