JavaScript 中的"双波浪线"(~~) 运算符是什么?

javascriptweb developmentfront end technology

"双波浪线"(~~) 运算符是双非位运算符。使用它替代 Math.floor(),因为它更快。

示例

您可以尝试运行以下代码来了解双波浪线运算符 −

<html>
   <body>
        <script>
         var a = 2;
         var b,c, d;

          b = ~~a;
         c = Math.floor(a);
          d = ~~b=== c;

         document.write(b);
         document.write("<br>"+c);
         document.write("<br>"+d); // 它们是相等的
      </script>
   </body>
</html>

相关文章