According to MDN Logical Operators page:
false && anything is short-circuit evaluated to false.
Given this information, I would expect false && true || true
to evaluate to false. However, this is not the case. The expected result (false
) is only given when the statement is written like:
false && (true || true)
A coworker and I have tried to work this out and the closest thing we could come up with is that the statement is being evaluated by order of precedence. According to the MDN Operator Precedence logical-and
has a higher precidence over logical-or
, suggesting that the condition is evaluated as if false && true
were a single statement, which then moves on to determine the boolean condition of false || true
which is then true
. Written out, this would be:
(false && true) || true
Something is wrong here. It's either the documentation, the JavaScript parsing logic, or my interpretation.
I've added a bounty because none of the answers given truly understand the question. As stated above: the MDN Logical Operators page states exactly: "false && anything is short-circuit evaluated to false."
A test result that incorrectly indicates that the condition being tested for is not present when, in fact, the condition is actually present. For example, a false-negative HIV test indicates that a person does not have HIV when the person actually does have HIV.
There are many potential causes of a false positive result, including the following: Mislabelling at the point of collection and at the point of processing. This can be guarded against by robust processes such as rigorous sampling and laboratory protocols. Contamination during sampling and processing.
If the response time changes according to the delay, it is a genuine vulnerability. If the response time is constant or the output explains the delay, such as a timeout because the application didn't understand the input, then it is a false positive.
PAH-zih-tiv ... reh-ZULT) A test result that indicates that a person has a specific disease or condition when the person actually does not have the disease or condition.
Your confusion all comes down to a misunderstanding of precedence.
A mathematical analogue would be "Zero multiplied by anything equals zero." Consider the following expression:
0 x 100 + 5
In any programming language, or a decent calculator, this evaluates to 5. The "zero times anything" axiom is true - but the "anything" in this case is 100
, NOT 100 + 5
! To see why, compare it to this:
5 + 0 x 100
It doesn't matter whether you add the 5 at the beginning or the end - the operator precedence rules remove ambiguity from the statement.
In JavaScript boolean logic, &&
has higher precedence than ||
. Since each operator is commutative, writing
false && true || true
is exactly the same as writing
true || false && true
The language works by parsing an expression into abstract syntax tree. Your expression false && true || true
gets parsed into this:
||
/ \
&& true
/ \
false true
Only after building the AST, the short-circuited evaluation can take place. The
false && anything is short-circuit evaluated to false.
quote applies only to a valid sub-tree where false
and anything
are subtrees of &&
node, like this:
&&
/ \
false true
which means only the false && true
gets short-ciruit evaluated to false
and resulting false || true
is evaluated to true
You are looking at precedence wrong.
&& is done first, then || so what it looks like is how you wrote it:
(false && true) || true
So the MDN link is correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With