Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does "5.5 | 0 === 5" work? [duplicate]

Tags:

javascript

How does 5.5 | 0 === 5 work?

Is | 0 in this context the bitwise OR I suspect it to be? If so, why does it cut off everything after the point?

like image 806
ben Avatar asked Dec 01 '22 19:12

ben


1 Answers

Bitwise operators always coerce operands to 32-bit integers.

The operation is interpreted as

5.5 | (0 === 5)

which is

5.5 | false

which is coerced to

5.5 | 0

Now the 5.5 is converted to a 32-bit integer, so we have

5 | 0

which is 5.

The relational operators bind more tightly than the bitwise operators which can be confusing. If you want to compare the result of a bitwise operator (unlike, say, an addition or multiplication), you have to parenthesize explicitly.

like image 104
Pointy Avatar answered Dec 06 '22 20:12

Pointy