In javascript undo and undo ~~ doesn't make sense

  • 2020-03-30 02:33:59
  • OfStack

The operator ~, is the meaning of bit inverted, on the surface ~~ (take the inverse and then take the inverse) does not make sense, in fact, JS can be converted to integer floating point.

var myArray = new Array(); 

//Now I'm going to randomly pick an element out of 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; 

var j = 7.34; 
j = ~~j; 



As shown above, if there is no ~~, then the random result is a decimal, and is to remove the decimal part of the decimal, keep the integer. I =7, j=7. However, such a mechanism does not exist in C, where you cannot invert a float by bit, and in C you can use casts (which JS does not have, floating point to integer) to achieve the same purpose.
< img SRC = "border = 0 / / 20143617540 ">

Related articles: