Javascript,For循环:返回'i'值与存储的值不同

am *_*ath 0 javascript alert for-loop

在该循环中,alert(i)警报12和firebug显示10作为最终结果.

 for(var i=0;i<=10;i=i+2){
      document.write=i;
    }

    alert(i);
Run Code Online (Sandbox Code Playgroud)

Ree*_*eno 5

i在每次循环迭代后增加.当条件失败时<= 10,循环中断:

0 => loop gets executed, i incremented with 2
2 => loop gets executed, i incremented with 2
4 => loop gets executed, i incremented with 2
6 => loop gets executed, i incremented with 2
8 => loop gets executed, i incremented with 2
10 => loop gets executed, i incremented with 2
12 => loop breaks => i remains at 12
Run Code Online (Sandbox Code Playgroud)