[英]JavaScript Operator Precedence
According to Mozilla , the === operator has higher precedence than the || 根据Mozilla的说法,===运算符的优先级高于||。 operator, which is what I would expect.
运算符,这是我所期望的。
However this statement evaluates to the number 1, rather than false. 但是,此语句的计算结果为1,而不是false。
let x = 1 || 0 === 0; // x === 1;
You have to wrap in parens to get a boolean: 您必须包装括号以获得布尔值:
let x = (1 || 0) === 0; // x === false;
What gives? 是什么赋予了?
NOTE this is not a dup of this question, which does not have anything about equality operators - JavaScript OR (||) variable assignment explanation 注意这不是这个问题的重复,它没有任何关于相等运算符的内容-JavaScript OR(||)变量赋值说明
Higher operator precedence is like a parenthesis around the operands. 更高的运算符优先级就像操作数周围的括号。
let x = 1 || (0 === 0);
The second part gets never evaluated, because of the truthy value of 1
. 由于真实值为
1
,所以第二部分永远不会得到评估。
||
is a short circuit operator and conditions are evaluated from left to right. 是短路运算符,条件从左到右进行评估。
So here : left || right
所以这里:
left || right
left || right
, if the left
condition is true
, the whole condition is evaluated to true
and the right
one is never evaluated. left || right
,如果left
条件为true
,则将整个条件评估为true
,而从不评估right
条件。
Here : 这里 :
let x = 1 || 0 === 0; // x === 1;
x = 1
assigns 1
to x
and the second condition after ||
x = 1
受让人1
至x
和之后的第二条件||
is never evaluated as if (1)
is evaluated
to true
. 永远不会被评估为
if (1)
被evaluated
为true
。
And here : 和这里 :
let x = (1 || 0) === 0; // x === false;
(1 || 0)
is evaluated to true
as if (1)
is still evaluated
to true
. (1 || 0)
被评估为true
, if (1)
仍被evaluated
为true
。
And then true === 0
is evaluated to false
. 然后将
true === 0
评估为false
。
So x
is valued to false
. 因此
x
的值为false
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.