首页 > 解决方案 > 在 javascript 中使用 while 循环有什么副作用?

问题描述

这两个 javascript while 循环是完全一样的。但是,一旦我们在其中一个之后删除 'console.log('end')',它会在 Chrome 控制台和 Node.js v14.8.0 中吐出额外的 '41' 数字。这就是所谓的“副作用”吗?

// While-loop 1
const max = 40, min = 30;
let i = min + 1;
while (i < max) {
  console.log(i);
  i += 2;
}
console.log('end');
// 31 33 35 37 39

// While-loop 2
const max = 40, min = 30;
let i = min + 1;
while (i < max) {
  console.log(i);
  i += 2;
}
// 31 33 35 37 39 41

标签: javascriptwhile-loop

解决方案


推荐阅读