最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Evaluation order of logical ANDs and ORs - Stack Overflow

programmeradmin2浏览0评论

I've got two expressions in the debugger watch (at the same time):

item.isSelected() === true: false

And at the same time:

(item.isSelected() === true) && !(item.workflowStatusCode.toUpperCase() === "INRV") && (item.productStatusCode.toUpperCase() !== "ACTV") || (item.workflowStatusCode.toUpperCase() === "INPR"): true

Why the second one evaluates to true when first is false?

ADDITION:

From here

Short-circuit evaluation

As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:

false && anything is short-circuit evaluated to false.

... it kinda implies that once first expression is evaluated to false the rest should have been skipped... I am still not clear what the rules are behind all this.

I've got two expressions in the debugger watch (at the same time):

item.isSelected() === true: false

And at the same time:

(item.isSelected() === true) && !(item.workflowStatusCode.toUpperCase() === "INRV") && (item.productStatusCode.toUpperCase() !== "ACTV") || (item.workflowStatusCode.toUpperCase() === "INPR"): true

Why the second one evaluates to true when first is false?

ADDITION:

From here

Short-circuit evaluation

As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:

false && anything is short-circuit evaluated to false.

... it kinda implies that once first expression is evaluated to false the rest should have been skipped... I am still not clear what the rules are behind all this.

Share Improve this question edited Oct 11, 2013 at 20:51 G. Stoynev asked Oct 11, 2013 at 20:06 G. StoynevG. Stoynev 7,7917 gold badges39 silver badges50 bronze badges 2
  • 1 Apparently item.workflowStatusCode.toUpperCase() === "INPR" is evaluating to true, and it’s being ORed with the other part of your expression, so the overall expression is true. – bdesham Commented Oct 11, 2013 at 20:07
  • So they are being evaluated left-to-right, where each evaluated pair bees the result for the next evaluation ? Or it has to do with precedence as Matt Ball suggests? (if so, how) – G. Stoynev Commented Oct 11, 2013 at 20:18
Add a ment  | 

2 Answers 2

Reset to default 7

|| has lower precedence than &&, so it's almost certainly the case that this is true:

(item.workflowStatusCode.toUpperCase() === "INPR")

Precedence can be viewed like this.

NOT higher than AND higher than OR

This means that:

A && !B && C || D

Evaluates like this:

( (A && (!B)) && C ) || D

The order of evaluation is then:

  1. !B
  2. A && {1}
  3. {2} && C
  4. {3} || D

Therefore if D is true nothing else matters and the expression evaluates to true.

As an aside, if you put D || at the beginning then lazy evaluators will ignore the rest if D is true.

发布评论

评论列表(0)

  1. 暂无评论