如何在JavaScript中计算此表达式

如何在JavaScript中计算此表达式,javascript,Javascript,我知道我在此表达式中使用的运算符优先级: if (typeof day === "undefined" || notifiedday !== weekday) //do something ==10 |5 ==10 来源 < P> >我知道C++中 >在代码执行< /强>中,这个表达式将这样工作: if (typeof day === "undefined") { if(notifiedday !== weekday) { //do something } } 我仍然不

我知道我在此表达式中使用的运算符优先级:

if (typeof day === "undefined" || notifiedday !== weekday) //do something
==10
|5
==10

来源

< P> >我知道C++中<强> >在代码执行< /强>中,这个表达式将这样工作:

if (typeof day === "undefined")
{
  if(notifiedday !== weekday)
  {
   //do something
  }
}

我仍然不确定这在运行时如何在JavaScript中工作。

当结果清楚时,比较就结束了

if (false && true) {  } //true won't be evaluated since the left side of the operation gave the final result

if (true || false) {  } //false won't be evaluated since the left side of the operation gave the final result
对于multiples语句也是如此

if (false || (false && true)) {  } // both falses are evaluated, the true won't be.

在JS中与C++一样。虽然您添加到问题中的示例是错误的,但应该是:

if (typeof day === "undefined"){ // do something }
else if(notifiedday !== weekday){ // do same thing }
你写的东西和我的一样

if (typeof day === "undefined" && notifiedday !== weekday){ //do something }
或功能 由于使用了or,因此表达式只需其中一个为true。因此,如果
typeof day==“undefined”
为真,则
notifiedday!==工作日
不需要检查,但如果
typeof day==“undefined”
为false,则需要检查这两个项目

和功能
由于使用了and,因此表达式必须同时为真。因此,如果
typeof day==“undefined”
为false,则
notifiedday!==工作日
不需要检查,因为和都需要,但是如果
typeof day==“undefined”
为真,那么这两项都需要检查。

@Teemu我知道,我写了数字near@Alnitak您的意思是执行上下文之间没有区别,代码段是不等价的。后者相当于逻辑,而在JS.sorry,我不清楚,我的意思是它和C++一样工作,虽然你正确的是,OP的C++片段本身是不正确的。是的,我可能用
if(typeof day === "undefined" || notifiedday !== weekday)
if(typeof day === "undefined" && notifiedday !== weekday)