2

What does | do in Javascript? Is it similar to the logical or operator ||? I have seen it being used to apparently convert strings to numbers. How does this work?

var x = '12345';
var num = x|0;
console.log(num);
Zamboni
  • 245
  • 2
  • 9

1 Answers1

2

It's a bitwise OR |. Sometimes misused (64 bit float vs 32 bit integer) for getting integer values.

var x = '12345.678',
    num = x | 0;

console.log(num);
iota
  • 34,586
  • 7
  • 32
  • 51
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324