js判断是否为数字

1. isNaN(有漏网之鱼)  null及空数组 及 只有一个数值的数组

isNaN({})//true
isNaN(1)
isNaN('1')
isNaN('') //false
isNaN( )//true
isNaN(null)//false
isNaN([])//false
isNaN([1])//false
isNaN(['1']) //false
isNaN('a')//true
isNaN([1,2])//true
isNaN(undefined)//true

2.parseFloat

parseFloat('A').toString()==NaN   //false
parseFloat('A').toString()=='NaN' //true
parseFloat('1').toString()=='NaN' //false
parseFloat(true).toString()=='NaN'  //true
parseFloat(' ').toString()=='NaN' //true
parseFloat(null).toString()=='NaN' //true
parseFloat([]).toString()=='NaN' //true

3.Object.prototype.toString.call()

Object.prototype.toString.call(1)  //"[object Number]"
Object.prototype.toString.call(null)  //"[object Null]"
Object.prototype.toString.call(undefined)  //"[object Undefined]"

4.正则

function isNumber(val){

    var regPos = /^d+(.d+)?$/; //非负浮点数
    var regNeg =/^-d+(.d+)?$/; //负浮点数
    if(regPos.test(val) || regNeg.test(val)){
        return true;
    }else{
        return false;
    }

}
原文地址:https://www.cnblogs.com/sunmarvell/p/15215374.html