What is "|" (pipes) in the JS array

I have a JS array, which in our existing code is used as follows:

temp = charArray[0 | Math.random() * 26];

I wanted to know what the use of "|" character in the above code and are there any other such operators?

+4
source share
2 answers

| bitwise OR , which means that all bits equal to 1 in any of the arguments will be equal to 1 as a result. A bitwise OR with 0 returns the given input, interpreted as an integer.

In your code, it is widely used to convert Math.random () a number to an integer. Bottom line:

var a = 5.6 | 0 //a=5
Run codeHide result
Explanation: Take

var a = 5; //binary - 101
var b = 6; //binary - 110

  a|b                a|a            a|0
  101                101            101
  110                101            000
 ------             ------         ------
  111-->7            101-->5        101-->5
Run codeHide result
+1
source

From MDN :

32 ( ) JavaScript.

32- () IEEE754 , ( , , 32 !).

temp = charArray[Math.floor(Math.random() * 26)];
+8

Source: https://habr.com/ru/post/1589000/


All Articles