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.
-
1
Apparently
item.workflowStatusCode.toUpperCase() === "INPR"
is evaluating totrue
, and it’s being ORed with the other part of your expression, so the overall expression istrue
. – 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
2 Answers
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:
!B
A && {1}
{2} && C
{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
.