js中的"~~"和"|"

时间:2021-05-24 08:51:19   收藏:0   阅读:0

~~ (取整)

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

如果你想使用比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

原文链接:
https://www.cnblogs.com/ll15888/p/11996989.html

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!