It's a bitwise operator... to quote MDN on Javascript operators and expressions:
Bitwise operators treat their operands as a sequence of 32 bits (zeroes and ones), rather than as decimal, hexadecimal, or octal numbers. For example, the decimal number nine has a binary representation of 1001. Bitwise operators perform their operations on such binary representations, but they return standard JavaScript numerical values.
What happens, is that the operator treats the number as a 32 bit integer; so 5.123
is treated as:
0000 0000 0000 0000 0000 0000 0000 0101
(the decimal part is thrown out) and 0 is treated as
0000 0000 0000 0000 0000 0000 0000 0000
Then the OR
function compares the two, and writes a 1
if either number has a 1
.
So using the bitwise OR
with a decimal number and zero is essentially a way to discard the decimal part and retain the integer part.
Your other example with two is (insignificant 0
s have been removed):
0101 (5)
0010 (2)
---- ORed
0111 (7)
and the example with 4:
0101 (5)
0100 (4)
---- ORed
0101 (5)
You can use it to convert to discard the decimal part of a number - see Using bitwise OR 0 to floor a number