简体   繁体   English

如何(false == false == true)为真

[英]How can (false == false == true) be true

While I was programming I had an unexpected outcome in my if statement. 在我编程时,我的if语句中出现了意想不到的结果。

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" 我没有找到任何可以帮助我W3S的东西,并且真的想知道为什么这些警报“真实”

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

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 . 因为false实际上等于false That is why it is evaluated to true . 这就是它被评估为true

Second Case 第二个案例

false == (false == true)

is reduced to 减少到

false == false

because false is not equal to true . 因为false不等于true That is why the entire expression is true because false is equal to false . 这就是整个表达式为true原因,因为false等于false

The execution will start from left hand side. 执行将从左侧开始。

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

at first false== false is true. 起初false== false是真的。 Then true==true is true. 那么true==true是真的。

In second case, since you have using parenthesis () that will be executed at first. 在第二种情况下,因为您使用了将首先执行的括号()

false == true is false. false == true为false。

Then false == false is true. 然后false == false为true。

Is it true that false == true ? 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). 我认为很明显这不是真的,因此(false == true)false ,因此false == (false == true) (因为我们已经注意到第二部分是假的)。

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 . 仍然javascript保证评估顺序从左到右,因此该表达式与(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 ,等于true

 false == (false == true) 

false == true , this is false , which equals false . false == true ,这是false ,等于false

First line - from left to right it compares false == false which is true , then compared to true returns true 第一行 - 从左到右它比较false == false ,这是true ,然后比较true返回true

Second line - from left to right it compares (false == true) which is false , then comparison of false and false returns true 第二行 - 从左到右比较(false == true) ,这是false ,然后falsefalse比较返回true

Both are correct. 两者都是正确的。

Case 1: false == false == true 案例1: false == false == true
this is similar to (false == false) == true 这类似于(false == false) == true

The evaluation is false == false IS true and true == true IS true 评估为false == false IS truetrue == true IS true

Case 2: false == (false == true) 案例2: false == (false == true)
this is similar to (false == true) == false 这类似于(false == true) == false

The evaluation is false == true IS false and false == false IS true 评估为false == true IS falsefalse == false IS true

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM