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.
We know the result of Boolean
with exponentiation operator
in Python as following:
>>> 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
? If so, could the same behavior as above in 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 to0 ** 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 equaltrue
, if it is0
, it will loosely equalfalse
.Yes
If you use
===
all of those will befalse
though because0
is not the same asfalse
and1
is not the same astrue
.