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

According to the MDN Logical Operators page:

false && & something short circuit evaluates to false.

Given this information, I expect it to false && true || truebe evaluated as false. However, it is not. The expected result ( false) is set only when the instruction is written like this:

false && (true || true)

The employee and I tried to fix this, and the closest thing we could come up with is that the statement is ranked in order of priority. In accordance with the Operator Priority MDN logical-and has a higher accuracy over logical-or, assuming that the condition is evaluated as if there false && truewere which then proceeds to determine the logical condition false || true, which then true. It is written:

(false && true) || true

Something is wrong here. This is either documentation, JavaScript parsing logic, or my interpretation.

Edit:

I added generosity because none of the answers gave a real understanding of the question. As stated above: the MDN Logical Operators page says exactly: "false &&" all this is a short circuit evaluated as false. "

" " " "!

+4
12

.

" ". :

0 x 100 + 5

5. " " , "-" 100, NOT 100 + 5! , , :

5 + 0 x 100

, 5 - .

JavaScript && , ||. commutative,

false && true || true

,

true || false && true
+9

, .

& & , || , :

(false && true) || true

, MDN .

+9

, . false && true || true :

          ||
         /  \
       &&    true
      /  \
 false    true

.

false && & - false.

, false anything && node, :

       &&
      /  \
 false    true

, false && true short-ciruit, false, false || true true

+9

JavaScript :

1) false && true //evaluates to false
2) false || true //equals true. The first false is the result above

, JavaScript :

(function(){alert(1); return false; })() && 
(function(){alert(2); return true; })() || 
(function(){alert(3); return true; })()
+4

. -, MDN, / .

, :

false && & - false.

, . ( ).

false && true || true == true

, . && ||. && , ||

. , , . , .

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

, , "" . , , false && == false.

, - , , .

false && true || true , - . false && doSomething() .

+4

:

A * B + C

,

(A * B) + C

, && ,

A && B || C

, :

(A && B) || C

, Anything, , B, A - false. , - :

(false && takesALongTimeToDetermine()) || true

takesALongTimeToDetermine()

: . . && true, true, , , false, false. ||: true, true . , - . , MDN, , && , , , . ( , takesALongTimeToDetermine() -, , , IE, , , . , &&.)

+2

, MDN .

- -, &&, &&.

&& 2 : && .

: false && true || true, false - , - . || true , &&.

, , , .

, (true) false && true ( || true )

, , :

  • &&
  • 2 &&,
  • (false) false
  • (true) , -, false
  • false && true false
  • || ( false && true false, false || true)
  • (false) false
  • (true) true
  • false || true true.
  • : true
+1

, , .

, : false && true || true true false.

, false || true , , true

"" MDN .

0

, , , OR , AND, :

(false && true) || true

, false || true true.

0

False boolean = 0;

True boolean = 1;

"& &" = ;

"||" = ;

? , :

if(true && false || true)

:

((1 x 0) + 1)
0

, , - , true. , , anything .

-

          ||
      /        \
     &&        true
   /    \
false   anything

anything, true .

. .

0

"false" "anything" "false && anything" / Javascript ( @Erbureth), . , :

Javascript, " " ( )

var a=false; a && 3
!1 && alert("Never!")
(1==2) && "x"

Javascript code that is not a “short circuit” (one per line)

<script>false && </script><p>Other page content...</p></body></html>
false && * x^.++@; true
"false && true"

We hope that these unaudited examples will help clarify why it makes no sense to apply the rule that you quote in the source code. The rule is intended to apply to the value / subtrees that arise after parsing the source code (from which the operation time order was applied)

-2
source

Source: https://habr.com/ru/post/1523651/


All Articles