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
  • 285
  • 3
  • 11

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);
Unmitigated
  • 46,070
  • 7
  • 44
  • 60
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358