javascript while loop giving different results in node and browser

后端 未结 2 604
野趣味
野趣味 2021-01-27 10:49

I am executing the following seemingly straightforward code

var number = 0;
while (number <= 12) {
    console.log(number);
    number = number + 2;  
}
         


        
2条回答
  •  無奈伤痛
    2021-01-27 11:23

    If you run a script in Firebug's console, then it will evaluate the code. So it's evaluating the value of the last number in the while loop (which is now 14) and prints that out. It's actually printing out the value of number 8 times but groups them in 1 print out.

    You can see the same effect by just typing in "window" in the command line. It's evaluating it's value and prints it out in the console.

    More info can be found in the description to Firebug's command line.

提交回复
热议问题