I've seen the operator |=
in a jQuery plugin, in the following line:
if (e.ctrlKey) mod |= 1;
How it works? Is it a good practice to use it?
I've seen the operator |=
in a jQuery plugin, in the following line:
if (e.ctrlKey) mod |= 1;
How it works? Is it a good practice to use it?
The |
operator is a bitwise OR. It essentially performs a logical OR operation on corresponding pairs of bits in its arguments. If any bit is 1
, the resulting bit is also 1
. E.g.:
00101
| 10100
= 10101
a |= b
is simply shorthand for a = a | b
, similar to several other binary operators in JavaScript.
It is often useful for setting binary configuration flags.