`let` vs. `const` vs. for-of 循​​环中的空

`let` vs. `const` vs. nothing in a for–of loop

在JavaScript中做以下三个构造有什么区别:

let dd = [ 1, 2, 3, 4, 5 ];

for(const item of dd) console.log(item);
for(let   item of dd) console.log(item);
for(      item of dd) console.log(item);

似乎它们都产生完全相同的结果,所以想知道它们之间是否存在一些细微差别,尤其是当 letconst 都不存在时,特别是在 [= 的上下文中13=]–of循环.

这里有一些例子可以说明区别:

let dd = [1, 2, 3, 4, 5]

// identically-named variable in the outer scope
let item = -1

for (const item of dd) {
    console.log(item) // ok
    // console.log(++item) // can't re-assign - will throw if uncommented
}

console.log(item) // still -1, as `const` is block-scoped

for (let item of dd) {
    console.log(item) // ok
    console.log(++item) // it's fine to re-assign 
}

console.log(item) // still -1, as `let` is block-scoped

for (item of dd) {}

console.log(item) // 5, as we've now re-assigned the variable in the outer scope
// due to not using `const` or `let`