I'm fiddling around with bitwise operators in JavaScript and there is one thing I find remarkable.
The bitwise or operator returns 1
as output bit if one of the two input bits are 1
. So doing x | 0
always returns x
, because | 0
has no effect:
( 1 | 0 ) === 1
( 0 | 0 ) === 0
However, when I calculated Infinity | 0
, I got 0
. This is surprising in my opinion, because by the above one should get Infinity
. After all, ( x | 0 ) === x
.
I cannot find where in the ECMAscript specification this is explicitly defined, so I was wondering what exactly implies that ( Infinity | 0 ) === 0
. Is is perhaps the way Infinity
is stored in memory? If so, how can it still be that doing a | 0
operation causes it to return 0
whereas | 0
should not do anything?
(infinity | 0)
? Infinity is... infinite, it can't be computed by its very definition... :| – IdiophoneInfinity
appears to be a truthy value when you use it in an if-else statement, like one would expect. I just confirmed this: jsfiddle.net/LWBVd. Perhaps it has to do with the internal representation ofInfinity
. – Imogeneimojean0.1
is truthy,0.1 | 0
is falsy. – Alcheringa