What is the "double tilde" (~~) operator in JavaScript?

The "double tilde" (~~) operator is a double NOT bitwise operator in JavaScript. It converts a number to a 32-bit signed integer and truncates any decimal part, making it a faster alternative to Math.floor() for positive numbers.

How It Works

The tilde (~) operator performs bitwise NOT operation. When used twice (~~), it effectively converts a value to an integer by:

  • Converting the value to a 32-bit signed integer
  • Truncating decimal places (not rounding)
  • Returning the integer result

Example: Basic Usage

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

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

         document.write("Original: " + a);
         document.write("<br>Double tilde: " + b);
         document.write("<br>Math.floor: " + c);
         document.write("<br>Are equal: " + d);
      </script>
   </body>
</html>
Original: 2.7
Double tilde: 2
Math.floor: 2
Are equal: true

Difference with Negative Numbers

The key difference between ~~ and Math.floor() appears with negative numbers:

<html>
   <body>
      <script>
         var negative = -2.7;
         
         document.write("Original: " + negative);
         document.write("<br>~~ operator: " + ~~negative);
         document.write("<br>Math.floor: " + Math.floor(negative));
         document.write("<br>Math.trunc: " + Math.trunc(negative));
      </script>
   </body>
</html>
Original: -2.7
~~ operator: -2
Math.floor: -3
Math.trunc: -2

Comparison Table

Input ~~ operator Math.floor() Math.trunc()
2.7 2 2 2
-2.7 -2 -3 -2
5.9 5 5 5

Performance Note

While ~~ was historically faster than Math.floor(), modern JavaScript engines have optimized Math.floor() significantly. The performance difference is now negligible, and Math.floor() or Math.trunc() are more readable.

Conclusion

The double tilde (~~) operator truncates decimal places and works like Math.trunc(). While it's a clever shortcut, prefer Math.floor() or Math.trunc() for better code readability and clarity.

Updated on: 2026-03-15T21:59:20+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements