0

I am new to java-script and not able to understand the use of | operator in following code snippet

<!DOCTYPE html>
<html>
<body>

<p>Click the button to display a number between 0 and 1</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
     var a=Math.random();
     var x = ((a*2 | 0));  
    document.getElementById("demo").innerHTML = x; 

}
</script>
</body>

</html>
Prashobh K V
  • 153
  • 1
  • 1
  • 11

1 Answers1

1

That's bitwise OR operator

Performs the OR operation on each pair of bits. a OR b yields 1 if either a or b is 1. The truth table for the OR operation is:

Bitwise ORing any number x with 0 yields x. Bitwise ORing any number x with -1 yields -1.

Suresh Atta
  • 114,879
  • 36
  • 179
  • 284