最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How can (false == false == true) be true - Stack Overflow

matteradmin7PV0评论

While I was programming I had an unexpected oute in my if statement.

How in the world can this code alert true? I didn't found anything that could help me at W3S, and really would like to know why these alerts "true"

window.alert(false == false == true); //alerts true
window.alert(false == (false == true));//even this alerts true

While I was programming I had an unexpected oute in my if statement.

How in the world can this code alert true? I didn't found anything that could help me at W3S, and really would like to know why these alerts "true"

window.alert(false == false == true); //alerts true
window.alert(false == (false == true));//even this alerts true
Share Improve this question asked Apr 6, 2015 at 12:16 call-mecall-me 7269 silver badges18 bronze badges 6
  • Because (false == true) == false and false == false equal true – Oleksandr T. Commented Apr 6, 2015 at 12:18
  • Operator Precedence. – epascarello Commented Apr 6, 2015 at 12:19
  • Does it help if I point out that you also cannot write 2 < 3 < 4, but instead you need to write 2 < 3 && 3 < 4? – deceze Commented Apr 6, 2015 at 12:20
  • To save you some pain in the future, you should know you don't have to keep using window.alert to debug stuff. In Chrome, ctrl+shift+j will open the dev console where you can evaluate javascript. In Firefox, ctrl+shift+i. Or just use Node's REPL and don't even bother with a browser. – Chris Martin Commented Apr 6, 2015 at 13:02
  • Also MDN is vastly preferred to W3S. – OrangeDog Commented Apr 6, 2015 at 13:06
 |  Show 1 more ment

6 Answers 6

Reset to default 9

First Case

false == false == true

will be evaluated as

(false == false) == true

because expressions are evaluated from left to right, by default. which reduces to

true == true

since false is actually equal to false. That is why it is evaluated to true.

Second Case

false == (false == true)

is reduced to

false == false

because false is not equal to true. That is why the entire expression is true because false is equal to false.

The execution will start from left hand side.

window.alert(false == false == true); 

at first false== false is true. Then true==true is true.

In second case, since you have using parenthesis () that will be executed at first.

false == true is false.

Then false == false is true.

Is it true that false == true? I think it is apparent that this is not true, thus (false == true) is false and therefor false == (false == true) (as we've already noticed that the second part is false).

As for the first example - actually no matter the order of evaluation it will hold true(I leave that to you as a logical exercise). Still javascript guarantees the evaluation order to be left to right thus this expression is the same as(false == false) == true. Again I leave to you to prove this is true.

false == false == true

false == false, this is true, which equals true.

false == (false == true)

false == true, this is false, which equals false.

First line - from left to right it pares false == false which is true, then pared to true returns true

Second line - from left to right it pares (false == true) which is false, then parison of false and false returns true

Both are correct.

Case 1: false == false == true
this is similar to (false == false) == true

The evaluation is false == false IS true and true == true IS true

Case 2: false == (false == true)
this is similar to (false == true) == false

The evaluation is false == true IS false and false == false IS true

Hope this helps

Post a comment

comment list (0)

  1. No comments so far