Explain Math.floor(Math.random())

前端 未结 6 1184
轻奢々
轻奢々 2020-12-19 07:52

I have seen many places using Math.floor() and Math.random()

like below

$(\'a.random-color\').hover(function() { //mouseove         


        
6条回答
  •  一整个雨季
    2020-12-19 08:14

    The Math.floor() is to drop the decimal portion of the Number. It is the opposite of Math.ceil().

    You can also double the invert bitwise operator (~~) to achieve the same as Math.floor() (though of course the floor() method is much more readable to most).

    ~~(Math.random() * 256)
    

提交回复
热议问题