简体   繁体   English

为什么JavaScript在非布尔&&语句中选择较大的数字?

[英]Why does JavaScript choose the larger number in non-boolean && statements?

in JavaScript, 0 && 1 evaluates to 0 , which is the lower of the two. 在JavaScript中, 0 && 1计算结果为0 ,这是两者中的较低者。 Why, then, does 0.1 && 1 evaluate to 1 , which is the higher of the two? 那么,为什么0.1 && 1求和为1 ,这是两者中的较高者? Similarly, why does 0 || 1 同样,为什么0 || 1 0 || 1 evaluate to 1 , but 0.1 || 1 0 || 1等于1 ,但0.1 || 1 0.1 || 1 evaluate to 0.1 0.1 || 1评估为0.1

It has nothing to do with which value is larger, the operators will return the appropriate value for the spec. 与较大的值无关,操作员将为规格返回适当的值。

In the case of && if the first parameter is false, it will be returned. &&的情况下,如果第一个参数为false,则将其返回。 Otherwise the second is returned. 否则,返回第二个。 In your example 0.1 && 1 , 0.1 is a truth-y value so 1 is returned. 在您的示例0.1 && 10.1是真实值-y,因此返回1 You could just as easily try 100000000 && 0.1 and see that 0.1 is returned. 您可以轻松尝试100000000 && 0.1然后看到返回了0.1 The reason that 0 && 1 returns 0 is because 0 is false-y so, per the spec, the first value gets returned. 0 && 1返回0的原因是因为0为false-y,因此根据规范,第一个值将被返回。

Likewise, with || 同样, || if the first parameter is true, it will be returned. 如果第一个参数为true,则将返回它。 Otherwise the second is returned. 否则,返回第二个。

You should check this out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators . 您应该查看https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators

Basically the && will take the first of the two if it is falsey otherwise it will take the second. 基本上,&&如果错误,将采用两者中的第一个,否则将采用第二个。

The opposite is true for ||. ||则相反。

The && and || &&|| operators do not return values based on inequalities such as < or > . 运算符不会基于不等式返回值,例如<>

a && b works as: a && b工作方式是:

if (a) {
    return b;
}
else {
    return a;
}

a || b a || b works as: a || b工作方式是:

if (a) {
    return a;
}
else {
    return b;
}

The if statements are based on the concept of "truthy" and "falsey" values, where 0 , NaN , null , undefined , '' , and false are all "falsey". if语句基于“ truthy”和“ falsey”值的概念,其中0NaNnullundefined''false均为“ falsey”。 All other values are "truthy". 所有其他值都是“真实的”。

0 && 1 evaluates to 0 because 0 is falsey. 0 && 1值为0因为0为假。
0.1 && 1 evaluates to 1 because 0.1 is truthy. 0.1 && 1值为1因为0.1是正确的。

There are several different things going on: 有几种不同的情况:

0 is bitwise false , and any number other than 0 evaluates to true , so the expression 0 && 1 evaluates to false && true , which is of course false . 0按位是false ,除0以外的任何数字都等于true ,因此表达式0 && 1等于false && true ,这当然是false

1 is bitwise true , and as per above any number <> 0 also evaluates to true , so 0.1 && 1 evaluates to true && true , which is true . 1是按位true ,并且根据上面的任何数字,<> 0的结果也为true ,因此0.1 && 1true && true ,这是true

Using the information above: 使用以上信息:

0 || 1 0 || 1 evaluates to false || true 0 || 1计算为false || true false || true , which is true false || true ,这是true

The final example is perhaps the most interesting one, and it has to do with operator short-circuiting. 最后一个示例也许是最有趣的示例,它与操作员短路有关。

The logical or operator ( || ) short-circuits, or stops evaluating, as soon as it encounters a value that evaluates to true . 逻辑或运算符( || )遇到一个评估为true的值时,就会短路或停止评估。 Thus, if you are using logical or in an assignment operation, it will return the first true value in the expression. 因此,如果您使用逻辑或赋值操作,它将返回表达式中的第一个true值。

Thus, 0.1 || 1 因此, 0.1 || 1 0.1 || 1 returns 0.1. 0.1 || 1返回0.1。 But, if you were to evaluate 1 || 0.1 但是,如果您要评估1 || 0.1 1 || 0.1 , it would instead return 1. 1 || 0.1 ,它将返回1。

As a related aside, the logical and operator ( && ) will short-circuit as soon as it encounters a value that evaluates to false . 另外,逻辑和运算符( && )遇到一个计算结果为false的值时就会短路。

You're specifically discussing logical operators, which actually do not care about the numbers themselves. 您正在专门讨论逻辑运算符,而逻辑运算符实际上并不关心数字本身。 Your code could be rewritten as 您的代码可以重写为

  0 && 1 - false && true
0.1 && 1 - true && true
  0 || 1 - false || true
0.1 || 1 - true || true

I assume you're setting this to a variable like x or something. 我假设您将其设置为x类的变量。 When you set it, the variable is being assigned as the first portion of the test that "fully" passes. 设置它时,变量将被分配为“完全”通过的测试的第一部分。

So, your cases above work like this: 因此,您上面的案例是这样的:

  1. The result is 0 because the test fails at 0 (0 && anything will fail). 结果为0,因为测试在0处失败(0 &&任何都会失败)。
  2. The result is 1 because the test "fully" passes after checking the right-hand side of the equation. 结果为1,因为在检查方程式的右侧后,测试“完全”通过了。 It checks that 0.1 is "truthy" and then checks that 1 is "truthy" and returns 1 since that was the last piece checked. 它检查0.1是否为“真”,然后检查1为“真”,并返回1,因为这是最后检查的部分。
  3. The result is 1 because the boolean logic checks 0 first, and then says "or 1". 结果为1,因为布尔逻辑首先检查0,然后说“或1”。 The "or 1" piece passes, so it returns 1. “或1”段通过,因此返回1。
  4. The result is 0.1 because that is "truthy" and it doesn't need to check the second side of the equation. 结果为0.1,因为那是“真实的”,并且不需要检查方程式的第二面。

Since this has nothing to do with the size of the numbers, just whether or not they are 0 or not 0, you can actually show this more clearly with using something like the following: 由于这与数字的大小无关,而与数字的大小(是否为0)无关,因此实际上可以使用类似以下的内容更清楚地显示此内容:

  0 && -1 - false && true
0.1 && -2 - true && true
  0 || -3 - false || true
0.1 || -4 - true || true

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

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