[英]Exponentiation operator for Boolean in JavaScript?
Refer to this , the exponentiation operator
returns the result of raising first operand to the power second operand, like the exponentiation operator in Python, which is part of the ECMAScript 2016 (ES7) proposal. 参照此 ,
exponentiation operator
会将加高第一个操作数的结果返回到幂第二个操作数,就像Python中的幂运算符一样,这是ECMAScript 2016(ES7)提议的一部分。
We know the result of Boolean
with exponentiation operator
in Python as following: 我们知道Python中使用
exponentiation operator
的Boolean
结果如下:
>>> False ** False == True
True
>>> False ** True == False
True
>>> True ** False == True
True
>>> True ** True == True
True
I want to know whether the Boolean
could be used in the exponentiation operator
? 我想知道
Boolean
exponentiation operator
是否可以在exponentiation operator
? If so, could the same behavior as above in Python? 如果是这样,是否可能与Python中的行为相同?
I'm not sure what kind of answer you expect. 我不确定您期望什么样的答案。 If you look at proposal you will notice that both operands are converted to numbers first.
如果查看提案,您会注意到两个操作数都首先转换为数字。 That means
false ** false
is equivalent to 0 ** 0
. 这意味着
false ** false
等于0 ** 0
。
So yes, you can apply the operator to Booleans. 所以是的,您可以将运算符应用于布尔值。 Just like with all the other operators, the values are converted to the type that the operator expects.
与所有其他运算符一样,这些值将转换为该运算符期望的类型。
The result will always be a number. 结果将始终是数字。
However, of course if you use loose comparison, then if the result of the exponentiation is 1
, it will loosely equal true
, if it is 0
, it will loosely equal false
. 但是,当然,如果使用松散比较,则如果求幂的结果为
1
,则将大致等于true
;如果为0
,则将大致等于false
。
console.log(false ** false == true); // true
console.log(false ** true == false); // true
console.log(true ** false == true); // true
console.log(true ** true == true); // true
If you use ===
all of those will be false
though because 0
is not the same as false
and 1
is not the same as true
. 如果您使用
===
所有这些都将为false
因为0
与false
而1
与true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.