Friday, 9 June 2017

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




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?


Answer



That ~~ is a double NOT bitwise operator.



It is used as a faster substitute for Math.floor().


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...