199
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
  • 16,643
  • 33
  • 95
  • 140
  • 21
    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

5 Answers5

194

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
  • 28,495
  • 9
  • 107
  • 102
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 12
    that's an nice way to convert floating-point number to int, or use `parseInt()` – MaBi Mar 14 '15 at 16:14
  • 7
    @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
  • 6
    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
  • 1
    Large numbers will overflow because they're converted to 32-bit int. – slikts May 12 '18 at 16:24
  • 1
    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
  • Use Math.trunc() to trim off the decimal places. Using the | is dangerous because of overflow errors. Really nefarious bug to hunt for. – Sean Dec 07 '22 at 18:07
166

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,480
  • 4
  • 23
  • 30
  • 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
22

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Yahel
  • 37,023
  • 22
  • 103
  • 153
15

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
  • 9,564
  • 146
  • 81
  • 122
Nikhil Mahirrao
  • 3,547
  • 1
  • 25
  • 20
0

This is a Bitwsie OR (|).

The operands are converted to 32-bit integers and expressed by a series of bits (zeroes and ones). Numbers with more than 32 bits get their most significant bits discarded.

So, in our case decimal number is converted to interger 0.5 to 0.

= 0.5 | 0
= 0   | 0
= 0
pradip garala
  • 482
  • 8
  • 7