js ~~ and | take a look

~~ It represents the double non-bitwise negation operator,

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

It should be noted that for positive numbers, it is rounded down; for negative numbers, it is rounded up; the value of non-digits is 0, and its specific expression is:

~~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 round

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 blog.csdn.net/weixin_43844696/article/details/108831442