作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么这个 while 循环在最后打印“1”?我只希望它打印 console.log 语句。使用 Codecademy 时看到。
for (i = 0; i < 2; i++) {
console.log("I understand for loops twice..lol");
};
var whileUnderstand = 0;
while(whileUnderstand < 2) {
console.log("I understand while loops twice..lol");
whileUnderstand++;
}
那个问题没有我问的问题的直接答案。此外,它只包含 console.log 语句而不是循环。主要是,没有回答说“控制台只是输出语句的最后评估值”。这是解决我的问题的答案。
最佳答案
只有在浏览器控制台中运行代码时才会发生这种情况。
是这条线引起的:
whileUnderstand++;
控制台只是输出语句的最后评估值。
只有 1
被记录而不是 0
的原因是在 console.log()
调用之外,只有最后一个语句已记录。
例如,如果我有以下代码片段,则仅记录 "d"
:
var a = "a";
var b = "b";
a = "c";
b = "d";
关于javascript - 基本 While 循环查询 - 为什么记录 "1"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31655600/
我是一名优秀的程序员,十分优秀!