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
  • 315
  • 1
  • 3
  • 12

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
  • 76,500
  • 11
  • 62
  • 80
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392