当前位置:首页 > Web开发 > 正文

js中~~和

2024-03-31 Web开发

~~它代表双非按位取反运算符,,

如果你想使用比Math.floor()更快的方法,那就是它了。

需要注意,对于正数,它向下取整;对于负数,向上取整;非数字取值为0,它具体的表现形式为:

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

| 的用法,通常用来取整

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

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/41417.html