Why is the result of the execution of the following code different?

for (var iTunes 1; I < = 5; iPP) {
setTimeout (function timer () {
console.log (i);
}, iTun1000);
}
and
for (let iTunes 1; I < = 5; iPP) {
setTimeout (function timer () {
console.log (i);
}, iTunes 1000);
}
why the execution results are different, and what is the principle of their implementation?

Jul.29,2021

is actually a matter of scope. The above is constantly changing the I value in the same space, and the following is putting an I value


a variable defined by var in each independent space. The scope is the whole closed function and is global. The scope of a variable defined by let is at the block level or in a subblock.
-No matter what line the variable declared by var is in the current scope, it is promoted to the header of the scope.
-the variable declared by var is promoted to the top of the scope and initialized, while the variable declared by let is not initialized at the top of the scope


let is different from var. The variable declared by let produces a block-level scope. Valid, for (var i = 0 in this block of code actually declares a global variable, and


declares global variables with var. On the other hand, es6 has a brand-new block-level scope, and it uses timeout calls, asynchronous operations, which will be executed at last, mainly the problem of scope. Brothers can take a look at Ruan Yifeng's es6 written very well

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ead03a-2122.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ead03a-2122.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?