The operator ~ means bitwise inverse. On the surface, ~~ (inverse and inverse) has no meaning, but in fact, floating-point numbers can be turned into integers in JS.
The code copy is as follows:
<html>
<script>
var myArray = new Array();
myArray.push("a");
myArray.push("b");
myArray.push("c");
myArray.push("d");
//Now you need to randomly extract an element from the array
var random = myArray[~~(Math.random()*myArray.length)]; //Math.random() returns a pseudo-random number between 0 and 1, which may be 0, but is always less than 1, [0 ,1)
var i = 7.94;
i = ~~i;
alert(i);
var j = 7.34;
j = ~~j;
alert(j);
</script>
</html>
As mentioned above, if there is no ~~, then the random decimal is obtained, and the decimal part of the decimal is removed and the integer is retained. As above i=7, j=7. However, this mechanism does not exist in C. In C, a float cannot be inverted bit by bit, and in C can be cast (while JS does not have this mechanism, floating point numbers are converted into integers. ) achieves the same purpose (discard the decimal part and keep the integer part).