• Variables inside a block are not externally accessible

``if(true){ let foo = "mcgee0731" } console.log(foo); //foo is not defined ... for(let i=0; i< 3; i++>){} console.log(i) //i is not defined``
• The effect of var on loops

``for(var i=0; i< 3; i++){ for(var i=0; i< 3; i++){ console.log(i); }} // When 'I = 3' goes to the outer cycle after the inner cycle, it cannot be re-entered. Print 0,1,2, 3 instead of 9``
``Var events = [{},{},{}] for(var I =0; i< events.length; I ++){events[I].onclick = function(){console.log(I)}} events[0].onclick() // Events [I].onclick(``
• Event counter bindings are handled through IIFE mode

``Var events = [{},{},{}] for(var I =0; i< events.length; i++){ events[i].onclick = (function(i){ return function(){ console.log(i) } })(i) } events[0].onclick() //0``
• Use let to solve the problem of the for loop

``for(let i=0; i< 3; i++){ for(let i=0; i< 3; i++){ console.log(i); }}... var events = [{},{},{}] for(let i=0; i< events.length; i++){ events[i].onclick = function(){ console.log(i) } } events[0].onclick() //0``
• There are two levels of scope in the for loop, so let’s break down the following method
``for(let i =0; i < 3; i++){ let i = "foo" console.log(i); } disassemble into... let i =0; // This is the scope of the for loop if(I <3){let I = "foo" // where n is inside the block,} I ++ will be executed three times``
• There is no variable promotion

``console.log(bar); // ReferenceError let bar = 2;``
• Let’s do a little example

Create STR =””+ I each time in the new lexical scope

``for(var i=0; i<1000; i++) { let str = "" str+=i }``
• Why block-level scoping

• Variable promotion resulted in an error
• Internal variable leak
• Anonymous immediate-execution function expressions (IIFE) are no longer necessary
• Functions are in block-level scope

• Allows functions to be declared at block-level scope.
• Function declarations are similar to var in that they are promoted to the head of the global scope or function scope.
• At the same time, the function declaration is promoted to the head of the block-level scope in which it resides.

#### const

• A read-only constant. Once declared, the value of a constant cannot be changed

``Const PI = 3.1415; PI = 3;``
• A const declaration does not return an error assignment

``const obj ``
• Const For an object, the address value of the object is bound to and cannot be changed. Property members within the address value can be changed.
• 6 methods of ES2015 declaration,`var function let const import class`
• We use const for the main and let for changes, not var