I was just experimenting and tried putting this in the console:
4 | 2 | 4 | 1 | 10
returns 15 in console..
4 | 2 | 4 | 3 | 1
returns 7 in console..
I tried that on Chrome and Firefox.
Why?
I’m just starting with learning JavaScript… maybe I’m missing a concept here?
The
|operator in JavaScript is a bitwise integer OR operator. So it’s doing an OR operation on the bits you’re giving it, resulting in 15.A bitwise OR operation takes each bit in the value and sets the corresponding bit in the result if either of the input bits in that position is set. So
4 is 0100 in binary 2 is 0010 4 is 0100 1 is 0001 10 is 1010 ---- 1111 = 15 decimalUpdate: In a comment on your question, you’ve said you were expecting
truerather than15. If so, you want the logical OR operator,||, not the bitwise operator, although||may also surprise you with what it returns (4 || 2 || 4 || 1 || 10 = 4, nottrue), as JavaScript’s logical OR (||) is curiously powerful, more so than in many other languages.