[英]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 && 1
, 0.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”值的概念,其中0
, NaN
, null
, undefined
, ''
和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 && 1
为true && 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: 因此,您上面的案例是这样的:
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.