我尝试在 for 循环中使用 while 循环,但每次尝试这样做时,我的浏览器都会崩溃。我知道当循环无限时会发生这种情况,但我似乎无法弄清楚为什么。
for(i=0;i < 2; i++)
{
console.log("hello");
while(i < 2)
{
console.log("it's me");
}
}
最佳答案
您将永远循环 while 循环,因为 i
保留该值并且内部永远不会更改。
我认为您可能会更好地使用 if 语句来获得额外的输出。
var i;
for (i = 0; i < 2; i++) {
console.log("hello");
if (i < 2) {
console.log("it's me");
}
}
关于javascript - for 循环内的 while 循环不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42959627/