What does this mean in javascript

I found this variable declaration in an open source project:

var index = ~~(i/BUFFER_LENGTH);

What does the “~~” mean? I can’t find answer on Google.

Thanks

Howdy,

The double tilde ~~ is a double NOT bitwise operator.
It is used as a faster substitute for Math.floor() (in that it just removes anything to the right of the decimal).

See here for a more indepth explanation: http://stackoverflow.com/questions/4055633/what-does-do-in-javascript

Thank you

No probs :slight_smile:

Just to expand on my above example:

Positive number:

var pi = 3.14159265359;
console.log(pi);  //3.14159265359
console.log(Math.floor(pi));  //3
console.log(~~pi); //3

Negative number:

var negPi = -3.14159265359;
console.log(negPi );  //-3.14159265359
console.log(Math.floor(negPi ));  //-4
console.log(~~negPi ); //-3