New to Javascript
This question already has an answer here:
It's a bitwise OR operator. But what it's doing there is using side-effects to make a number which may have a fractional portion a whole number instead.
All numbers in JavaScript are floating-point, so (init_num + last_num) / 2
may have a fractional portion. When you apply a bitwise operator to a number, it's temporarily coerced to a 32-bit integer, losing any fractional portion. Since the OR operator's result has a bit set for any bit set on either operand, and since the second operand in your example is 0 (all bits off), the result is the same bit pattern as the left-hand operand (which is then turned back into a floating-point number).
The pipe is a bitwise or.
One use of bitwise operators are numerical convertions, because sometimes they're much faster than their Math or parseInt equivalents. The price you pay is some code readability.
More information can be found here:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators
|
stands for OR
example: x = 5 | 1 0101 | 0001 0101 = 5
x = 5 | 1 0101 | 0001 0101 = 5
also have a look at this: js comparisons
链接地址: http://www.djcxy.com/p/77448.html下一篇: JavaScript新手