gpt4 book ai didi

javascript - 基本 While 循环查询 - 为什么记录 "1"?

转载 作者:行者123 更新时间:2023-11-30 07:36:23 25 4
gpt4 key购买 nike

enter image description here

为什么这个 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++;
}

enter image description here

那个问题没有我问的问题的直接答案。此外,它只包含 console.log 语句而不是循环。主要是,没有回答说“控制台只是输出语句的最后评估值”。这是解决我的问题的答案。

最佳答案

只有在浏览器控制台中运行代码时才会发生这种情况。

是这条线引起的:

whileUnderstand++;

控制台只是输出语句的最后评估值。

enter image description here


只有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com