Tag:Let and VaR in ES6

  • The difference between let and VaR in ES6 for loop

    Time:2020-8-2

    Let is different from var for(var i=0;i<5;i++){ setTimeout(()=>{ console.log (i) ; // five 5 },100) } console.log(i);//5 console.log(‘=============’) for(let j=0;j<5;j++){ setTimeout(()=>{ console.log(j);//0,1,2,3,4 },100) } console.log (j) ; // error J is not defined Why can let display the correct result, but not var? VaR is a global scope, which has the function of variable promotion. […]