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

The Question :

575 people think this question is useful

I’m seeing this in some code, and I have no idea what it does:

var jdn = function(y, m, d) {
  var tmp = (m <= 2 ? -1 : 0);
  return ~~((1461 * (y + 4800 + tmp)) / 4) + 
         ~~((367 * (m - 2 - 12 * tmp)) / 12) - 
         ~~((3 * ((y + 4900 + tmp) / 100)) / 4) + 
         d - 2483620;

What’s the ~~ operator do?

The Question Comments :
  • You’ve probably figured it out by now but it returns the number of days between the millennium and a given date
  • Google’d this when I saw it on codewars, only to find out google doesn’t support tildes. Thank you Duck Duck Go!
  • in simple words, it converts ‘9’ into a number 9. And its must faster than Math.floor() even with IE8 if you do the following in console: typeof ~~’9’//number
  • Not having comments is one thing but what I hate most is that someone named this jdn. I don’t even know what that stands for.
  • Math.trunc(), not Math.floor()

The Answer 1

738 people think this answer is useful

That ~~ is a double NOT bitwise operator.

It is used as a faster substitute for Math.floor() for positive numbers. It does not return the same result as Math.floor() for negative numbers, as it just chops off the part after the decimal (see other answers for examples of this).

The Answer 2

182 people think this answer is useful

It hides the intention of the code.

It’s two single tilde operators, so it does a bitwise complement (bitwise not) twice. The operations take out each other, so the only remaining effect is the conversion that is done before the first operator is applied, i.e. converting the value to an integer number.

Some use it as a faster alternative to Math.floor, but the speed difference is not that dramatic, and in most cases it’s just micro optimisation. Unless you have a piece of code that really needs to be optimised, you should use code that descibes what it does instead of code that uses a side effect of a non-operation.

Update 2011-08:

With optimisation of the JavaScript engine in browsers, the performance for operators and functions change. With current browsers, using ~~ instead of Math.floor is somewhat faster in some browsers, and not faster at all in some browsers. If you really need that extra bit of performance, you would need to write different optimised code for each browser.

See: tilde vs floor

The Answer 3

134 people think this answer is useful
~(5.5)   // => -6
~(-6)    // => 5
~~5.5    // => 5  (same as Math.floor(5.5))
~~(-5.5) // => -5 (NOT the same as Math.floor(-5.5), which would give -6 )

For more info, see:

The Answer 4

21 people think this answer is useful

The diffrence is very simple:

Long version

If you want to have better readability, use Math.floor. But if you want to minimize it, use tilde ~~.

There are a lot of sources on the internet saying Math.floor is faster, but sometimes ~~. I would not recommend you think about speed because it is not going to be noticed when running the code. Maybe in tests etc, but no human can see a diffrence here. What would be faster is to use ~~ for a faster load time.

Short version

~~ is shorter/takes less space. Math.floor improves the readability. Sometimes tilde is faster, sometimes Math.floor is faster, but it is not noticeable.

Add a Comment