type checking in javascript

后端 未结 8 900
Happy的楠姐
Happy的楠姐 2020-12-13 11:48

How can I check if a variable is currently an integer type? I\'ve looked for some sort of resource for this and I think the === operator is important, but I\'m not sure how

8条回答
  •  不知归路
    2020-12-13 12:34

    A number is an integer if its modulo %1 is 0-

    function isInt(n){
        return (typeof n== 'number' && n%1== 0);
    }
    

    This is only as good as javascript gets- say +- ten to the 15th.

    isInt(Math.pow(2,50)+.1) returns true, as does Math.pow(2,50)+.1 == Math.pow(2,50)

提交回复
热议问题