IF语句中的多个OR语句

时间:2014-11-11 15:18:32

标签: javascript

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

var first = true;
var third = false;

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

else语句每次都在这里进行评估,我很困惑为什么,首先是真的,没有变量叫做second,第三个也是false,只有一个必须为true才能触发if条件,对吗?

1 个答案:

答案 0 :(得分:2)

我认为你从localStorage得到一个字符串,并没有将它转换为布尔值。将字符串转换为布尔值,它应该可以工作。

Fiddle

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

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