What's the action scope of for-loop in ES6?

zyp*_*zyp 10 javascript for-loop

What's exactly the action scope of let in a for-loop in JavaScript?

for (let i = 0; i < 3; i++) {
  let i = 4;
  console.log(i);
}
console.log(i);
Run Code Online (Sandbox Code Playgroud)

The external console.log throws an error:

"Uncaught Reference Error: i is not defined"

It proves i is in a block action scope, however, why doesn't the i defined in the for-loop throw any duplicate definition error?

MrG*_*eek 0

使用 声明的变量在其作用域之外不可见(不可访问),这解释了在 for 循环之外的 thatlet引发的错误)。console.log

它还解释了为什么循环运行三次并打印4,解释是ifor 循环块内部(声明为 的那个let i = 4;)与 in 循环头不同i,因为关键字不可见let,即iin let i = 4;for 循环块内部的内容在 中不可见for (let i = 0; ...),它们是不同的,因此块内部的内容不会影响外部的内容(在标头中)。