如何高效判定是非负整数,类型是number
下面是群里的一些记录:
var num = -4; ( n| 0 ) === n //ssddi456 ng ( n >= 0 ) && ( n % 1 == 0 );// 司徒正美 ng var n ="33" parseInt(n) > 0 && n == Math.round(n)// 烦茄 ng typeof n == 'number' && n >0 && n == Math.round(n) //烦茄 ok n === n >> 0 && n >= 0 // im007boy ok var n = "22" console.log((~n | 0) == -Math.abs(-n -1)) // 烦茄 ng ( 0x80000000 & num ) //BruceWolf ng 这是C语言的hack ~~i === i && i >= 0 //刺猬 ok var num = -9 parseInt( num )=== num && num > 0 //BruceWolf ok