js in ~ and |

It represents ~~ bi-bitwise operators,

If you want to use a faster method than Math.floor (), that is it.

It is noted that for a positive number, it is rounded down; for negative rounding up; non-numeric value is 0, it is a specific form of:

~~null;      // => 0
~~undefined; // => 0
~~Infinity;  // => 0
--NaN;       // => 0
~~0;         // => 0
~~{};        // => 0
~~[];        // => 0
~~(1/0);     // => 0
~~false;     // => 0
~~true;      // => 1
~~1.9;       // => 1
~~-1.9;      // => -1

| Usage, usually used to rounding

1.2|0  // 1
1.8|0 // 1
-1.2|0 // -1
console.log(1553 / 10   | 0)  // Result: 155
console.log(1553 / 100  | 0)  // Result: 15
console.log(1553 / 1000 | 0)  // Result: 1

 

Guess you like

Origin www.cnblogs.com/ll15888/p/11996989.html
js