gpt4 book ai didi

javascript - 如何写If/else语句?

转载 作者:行者123 更新时间:2023-11-30 06:56:57 25 4
gpt4 key购买 nike

x = 1;    
if(x = 10) {x = 1;}
else {x = x + 1;}
alert (x);

结果总是1,而不是1,2,3...

最佳答案

替换

if(x = 10) {x = 1;} 

if(x == 10) {x = 1;} 

因为 x=10 返回 10,它在测试中评估为 true,因此代码 {x = 1;} 是执行。

来自 the MDN about if...else :

Any value that is not undefined, null, 0, NaN, or the empty string (""), and any object, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement

关于javascript - 如何写If/else语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13719274/

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