gpt4 book ai didi

javascript - IF 语句中包含多个 OR 语句

转载 作者:行者123 更新时间:2023-12-02 16:48:50 25 4
gpt4 key购买 nike

对这里发生的事情有点困惑:

var first = true;
var third = false;

if(first === true || !second || third === false) {
alert('if');
}
else{
alert('else');
}

else 语句每次都在这里求值,我很困惑为什么,第一个是 true,没有名为第二个的变量,第三个也是 false,只有一个必须为 true 才能触发 if 条件,对吧?

最佳答案

我认为您从 localStorage 获取一个字符串,并且没有将其转换为 bool 值。将字符串转换为 bool 值,它应该可以工作。

Fiddle

var first = 'true';  //string returned from localStorage
var third = 'false';

if (Boolean(first) === true || !second || Boolean(third) === false) {
alert('if');
}
else {
alert('else');
}

关于javascript - IF 语句中包含多个 OR 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26868164/

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