162
console.log(0.5 | 0); // 0
console.log(-1 | 0);  // -1
console.log(1 | 0);   // 1

Why does 0.5 | 0 return zero, but any integer (including negative) returns the input integer? What does the single pipe ("|") do?

smholloway
  • 589
  • 7
  • 14
Matrym
  • 15,021
  • 33
  • 90
  • 137
  • 14
    It helpfully prevents syntax errors from alerting you to the fact that you typed | instead of || – Andrew Myers May 18 '17 at 17:34
  • By employing a bitwise OR on a float in this manner, you are basically banking on the immaturity of JavaScript. Python3 would raise the error `TypeError: unsupported operand type(s) for |: 'float' and 'int'` – Serge Stroobandt Mar 30 '21 at 15:12

4 Answers4

166

This is a bitwise or.
Since bitwise operations only make sense on integers, 0.5 is truncated.

x | 0 is x, if x is an integer.

Serge Stroobandt
  • 19,748
  • 8
  • 84
  • 81
SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
  • 11
    that's an nice way to convert floating-point number to int, or use `parseInt()` – MaBi Mar 14 '15 at 16:14
  • 5
    @MaBi: You should however know that the value is converted to a 32-bit integer, so it won't work properly for larger numbers. – Guffa May 05 '15 at 17:51
  • 1
    So can be considered to be same as Floor function? – May13ank Aug 03 '15 at 17:37
  • 4
    Use this only for bitwise or. As @Guffa said, large numbers will not behave as expected. Ex: 248004937500 | 0 = -1103165668 – Joseph Connolly Mar 08 '17 at 17:38
  • Large numbers will overflow because they're converted to 32-bit int. – slikts May 12 '18 at 16:24
  • When people ask me what's the difference between `|` and `||` I always give them this example: `0.5|1` returns 1 and `0.5||1` returns 0.5. – thdoan Mar 30 '19 at 22:24
156

Bit comparison is so simple it's almost incomprehensible ;) Check out this "nybble"

   8 4 2 1
   -------
   0 1 1 0 = 6  (4 + 2)
   1 0 1 0 = 10 (8 + 2)
   =======
   1 1 1 0 = 14 (8 + 4 + 2)

Bitwise ORing 6 and 10 will give you 14:

   alert(6 | 10); // should show 14

Terribly confusing!

smholloway
  • 589
  • 7
  • 14
Trey
  • 5,325
  • 4
  • 21
  • 28
  • 17
    Works for Boolean as well. JS interprets true as 1, false as 0; so `alert(true | false) //yields 1; alert(true | true) //yields 1; alert(false | true) //yields 1; alert(false | false) //yields 0` – gordon Feb 18 '14 at 15:41
21

A single pipe is a bit-wise OR.

Performs the OR operation on each pair of bits. a OR b yields 1 if either a or b is 1.

JavaScript truncates any non-integer numbers in bitwise operations, so its computed as 0|0, which is 0.

Yahel
  • 35,856
  • 22
  • 98
  • 150
9

This example will help you.

var testPipe = function(input) { 
   console.log('input => ' + input);
   console.log('single pipe | => ' + (input | 'fallback'));
   console.log('double pipe || => ' + (input || 'fallback'));
   console.log('-------------------------');
};

testPipe();
testPipe('something'); 
testPipe(50);
testPipe(0);
testPipe(-1);
testPipe(true);
testPipe(false);
Pang
  • 8,605
  • 144
  • 77
  • 113
Nikhil Mahirrao
  • 2,467
  • 1
  • 17
  • 15