Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `false && true || true` evaluate to true?

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.

Edit:

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."

"anything" means "anything"!

like image 908
Kyle Falconer Avatar asked Jan 25 '14 02:01

Kyle Falconer


People also ask

What is false negative example?

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.

Why do false positives occur?

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.

How do you know a false positive?

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.

What does 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.


3 Answers

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
like image 83
BJ Myers Avatar answered Nov 05 '22 20:11

BJ Myers


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

like image 41
Erbureth Avatar answered Nov 05 '22 18:11

Erbureth


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.

like image 9
James Black Avatar answered Nov 05 '22 18:11

James Black