How to check if a JavaScript number is a real, valid number?

后端 未结 6 1520
礼貌的吻别
礼貌的吻别 2020-12-16 15:30

MY code is:

function isNumber(n){
return typeof n == \'number\' && !isNaN(n);
}

window.onload=function(){
var a=0,b=1,c=2.2,d=-3,e=-4.4,f=10/3;
var          


        
6条回答
  •  甜味超标
    2020-12-16 16:06

    If you consider a number in its object wrapper to be a number, then it will fail with:

    isNumber( new Number(123) )
    

    Since a downvoter is having some comprehension troubles that couldn't be alleviated by a simple test, new Number(123) will return 'object' from the typeof test, and as such will not pass.

提交回复
热议问题