简体   繁体   English

JS:为什么这返回true?

[英]JS: why does this return true?

0 + 1 === 1 || 1 + 1 === 2 || 2 + 1 === 10

Can someone please explain why this statement returns true ? 有人可以解释为什么该语句返回true吗?

Individually, the first two are true and the last one is false. 分别而言,前两个为true,最后一个为false。 However OVERALL , the statement returns true. 但是, 总体而言 ,该语句返回true。 It seems that you just need one true statement among a longer list of statements to make the OVERALL statement return true. 似乎您只需要一个较长的语句列表中的一个true语句即可使OVERALL语句返回true。 Is that correct? 那是对的吗?

Just trying to cement my understanding of Booleans. 只是试图巩固我对布尔值的理解。 Cheers. 干杯。

Well, || 那么, || returns the first truthy value if there is one so: 如果有,则返回第一个真实值:

1 || ANYTHING_IN_THE_WORLD ; // returns 1

So all you really have there is 所以你真正拥有的就是

0 + 1 === 1

Which is true. 没错 So yes, your understanding is correct. 是的,您的理解是正确的。


Expanding on execution order: 扩展执行顺序:

Operator precedence is in play here: 运算符优先级在这里起作用:

0 + 1 === 1 || 1 + 1 === 2 || 2 + 1 === 10

First we have addition: 首先,我们有:

1 === 1 || 2 === 2 || 3 === 10

Then we have equality checks: 然后我们进行相等性检查:

true || true || false;

Which as explained above true || ANYTHING_AT_ALL 如上所述,其中哪一个true || ANYTHING_AT_ALL true || ANYTHING_AT_ALL is true, so the final output is true . true || ANYTHING_AT_ALL为true,因此最终输出为true

|| is an or operator. or运算符。 So if condition one or condition two or condition 3 is true, the statement is true. 因此,如果条件一条件二条件3为真,则该语句为真。

I suggest reading up on JavaScript here: https://developer.mozilla.org/en/docs/Web/JavaScript 我建议在这里阅读JavaScript: https//developer.mozilla.org/en/docs/Web/JavaScript

it returns true because with OR operator (||) first true will return true. 它返回true,因为使用OR运算符(||),第一个true将返回true。

The evaluation will perform : 评估将执行:

> 1- is (0+1===1)

=> true! =>是的! Then returns true. 然后返回true。

This 这个

> 0+1===1 || 1+1000===1

will return true for the same reason. 出于相同的原因将返回true。

"some apples are purple OR some apples are green" is true because it is sufficient that some apples are green. “某些苹果是紫色或某些苹果是绿色”是正确的,因为某些苹果是绿色就足够了。 a OR b will be true when either or both are true. 当一个或两个为真时,或a OR b为真。 a AND b needs both to be true. a AND b都必须为真。

Thus, the fact that some of the items in your expression are true makes the whole expression true. 因此,表达式中的某些项目为真的事实使整个表达式为真。 It would not be if you replaced || 如果替换||不会 with && . &&

In fact, 事实上,

It seems that you just need one true statement among a longer list of statements to make the OVERALL statement return true. 似乎您只需要一个较长的语句列表中的一个true语句即可使OVERALL语句返回true。 Is that correct? 那是对的吗?

is (almost) exactly correct. 是(几乎)完全正确的。 The only correction I'd put in is replacing "true" with "truthy", at least in the context of JavaScript. 我要进行的唯一更正是至少在JavaScript上下文中将“ true”替换为“ truthy”。 For example, 3 || 5 例如, 3 || 5 3 || 5 is not true , but 3 - while 3 is not true , it is truthy. 3 || 5是不是true ,但3 -而3是不是true ,这 truthy。

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

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