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

While I was programming I had an unexpected oute in my if statement.How in the world can this code aler

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745630523a4637081.html

相关推荐

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

    While I was programming I had an unexpected oute in my if statement.How in the world can this code aler

    22天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信