js中判断是否为数字

来源:互联网 发布:中文linux操作系统 编辑:程序博客网 时间:2024/05/22 04:26

如果数字声明方式为

var n =2;var n = Number(22);//可以针对传入参数的类型,执行不同的类型转换过程

判断结果:

alert(n instanceof Number);//false alert(typeof n ==="number");//truealert(typeof n ==="object");//false`

如果数字声明方式是

 var n = new Number(12);//使用构造函数,此处将Number作用构造方法调用,返回的是Number类型的对象,该对象能够访问Number的原型属性以及方法;不推荐方式  alert(n instanceof Number);//true     alert(typeof n ==="number");//false     alert(typeof n ==="object");//true

可以使用全局函数isNaN(),检查是否非数值
可以用来检测parseFloat()和parseInt()的结果,以判断它们是否为有效数值
isNaN(0);

isNaN(0); //falseisNaN(parseInt("3"));//falseisNaN(parseInt("hello"));//trueisNaN("3");//falseisNaN("hello");//trueisNaN(undefined);//true     
0 0
原创粉丝点击