[英]JavaScript - Why does (0 || -0) return -0
I've tested the following code 0 || -0
我测试了以下代码
0 || -0
0 || -0
in the Chrome Developer Console and I've got back -0
as a result and I'm trying to figure out why. 在Chrome开发者控制台中
0 || -0
,我得到了-0
,因此我正在试图找出原因。 This table represents results in different JS environments. 此表表示不同JS环境中的结果。
╔══════════════════════════════════════════════════════╗
║ Browser ║ Version ║ 0 || -0 ║
╠══════════════════════════════════════════════════════╣
║Internet Explorer 11 ║ 11.3024.14393.0 ║ 0 ║
║Google Chrome ║ 75.0.3770.100 ║ -0 ║
║Mozilla Firefox (Quantum) ║ 67.0.4 ║ -0 ║
║Microsoft Edge (Chromium) ║ 77.0.197.1 ║ -0 ║
║Microsoft Edge (Chakra) ║ 44.17763.1.0 ║ 0 ║
║Node.js ║ 11.1.0 ║ -0 ║
╚══════════════════════════════════════════════════════╝
My assumption is that in the first case, when the -0
is on the right side, the result is -0
, however when it's on the left side, the result is 0
. 我的假设是,在第一种情况下,当
-0
在右侧时,结果为-0
,但是当它在左侧时,结果为0
。 This leads me to believe that when -0
is on the left side during the binary logical OR operation, the sign bit is discarded (for whatever reason) and the operation is performed like this 0 || 0
这让我相信当二进制逻辑OR操作期间
-0
在左侧时,符号位被丢弃(无论出于何种原因)并且执行操作,如此0 || 0
0 || 0
. 0 || 0
。 While, when it's on the right side, because the -
operator is present, it probably added a sign bit to the number, which results in the right side number having one extra bit, causing the right side value to be returned. 而当它在右侧时,因为
-
运算符存在,它可能在数字上添加了一个符号位,这导致右侧数字有一个额外的位,导致返回右侧值。
What's even more interesting is that when you use positive numbers, the behavior is different. 更有趣的是,当你使用正数时,行为是不同的。 Now the
-1 || 4
现在
-1 || 4
-1 || 4
returns -1
, while 4 || -1
-1 || 4
返回-1
,而4 || -1
4 || -1
returns 4
and 4 || -1 || 6
4 || -1
返回4
和4 || -1 || 6
4 || -1 || 6
4 || -1 || 6
returns 4
(shouldn't it return 6
). 4 || -1 || 6
返回4
(不应该返回6
)。 Is this behavior caused by the -
operator or is it because I'm testing this code in the developer console? 这种行为是由
-
运算符引起的还是因为我在开发人员控制台中测试此代码?
Per the official specification, a || b
根据官方规范,
a || b
a || b
evaluates to a
if a
is truthy, or b
if a
is falsey. a || b
评估a
a
是否真实,或b
如果a
是假的。 &&
works in a similar fashion - if the whole expression's value can be determined by just checking the value of the left-hand side, it will evaluate to the left-hand side, and will otherwise evaluate to the right-hand side. &&
以类似的方式工作 - 如果只通过检查左侧的值来确定整个表达式的值,它将评估到左侧,否则将评估到右侧。
Logical OR (||)
逻辑OR(||)
expr1 || expr2
If expr1 can be converted to true, returns expr1;
如果expr1可以转换为true,则返回expr1; else, returns expr2.
否则,返回expr2。
Because both 0
and -0
are falsey, 0 || -0
因为
0
和-0
都是假的, 0 || -0
0 || -0
should evaluate to -0
, because the left side was falsey. 0 || -0
应该评估为-0
,因为左侧是假的。
Comparing numbers other than 0
/ -0
will have different effects, because other non-zero numbers are not falsey (except for NaN
). 相比要比其他号码
0
/ -0
会有不同的效果,因为其他非零数字是不是falsey(除了NaN
)。
Perhaps IE and Edge-Chakra are simply ignoring the difference between 0
and -0
, and display 0
regardless. 也许IE和Edge-Chakra只是忽略
0
和-0
之间的差异,并且无论如何都显示0
。 For example, if you type -0
into IE's console, you get 0
as a result ( not -0
): 例如,如果在IE的控制台中键入
-0
,则结果为0
( 不是 -0
):
Edge does the same thing: Edge做同样的事情:
The difference may just be purely visual, though. 不过,差异可能只是纯粹的视觉效果。 The following snippet prints
true
, false
, in Edge, as expected: 以下代码段在Edge中按预期打印
true
, false
:
const x = -0; console.log(Object.is(x, -0)); console.log(Object.is(x, 0));
" binary logical OR " may be where the assumption gets problematic: there is bitwise/binary or, |
“ 二进制逻辑OR ”可能是假设出现问题的地方:有位/二进制或
|
, looking at the digits of the binary numbers, and logical or, ||
,查看二进制数字的数字,以及逻辑或,
||
, looking at each operand as a single true/false value, and thus they are rather different. ,将每个操作数视为单个真/假值,因此它们是相当不同的。
You have the latter one, that is why you get back one of the exact inputs and not a "combination" of them. 你有后一个,这就是为什么你得到一个确切的输入而不是它们的“组合”。 This is especially visible with
-1
, as in two's component code it is 111...1111
( 32 of them in case of JavaScript when bitwise operations are involved), and thus binary or-ing anything to it just reproduces -1
. 这在
-1
特别明显,因为在两个组件代码中它是111...1111
(在涉及按位操作的情况下是JavaScript的32个 ),因此二进制或任何东西只是再现-1
。 But in case of a logical or, you get back the first operand unchanged, if it is 'true-ish', or you get back the second operand, again unchanged. 但是如果是逻辑或者,你将第一个操作数保持不变,如果它是'true-ish',或者你得到第二个操作数,则再次保持不变。
(Of course the MS thing with the -0
described in the other answer is adding some confusion for sure, but it happens before, and independently from the logical operation) (当然,在另一个答案中描述的带有
-0
的MS事物肯定会增加一些混淆,但它发生在之前,并且独立于逻辑运算)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.