首页 > 其他 > 详细

Number 和 parseInt 区别

时间:2017-06-12 12:37:34      阅读:239      评论:0      收藏:0      [点我收藏+]

把字符串 转换成 数字的时候, Number 有点不靠谱, 因为会对 ‘‘ 和 null 转换成0, parseInt 相对靠谱些;

判断是否是数值时, isNaN 对于字符串‘2‘的判断是数字, 对 null 和 ‘‘ 也是数字, 所以也是不靠谱;

另外注意 typeof NaN 为 ‘number‘, 说明 typeof 判断数字也是不靠谱。

 

Number(‘‘); // 0

Number(null); // 0

Number(undefined); //NaN

 

parseInt(‘‘); // NaN

parseInt(null); // NaN

parseInt(undefined); // NaN

 

isNaN(2); // false

isNaN(‘2‘); // false

isNaN(null); // false, 由于 Number(null) -> 0

isNaN(‘‘); // false, 由于 Number(‘‘) -> 0

isNaN(undefined); // true

isNaN(NaN); // true

 

typeof NaN; // ‘number‘

 

Number 和 parseInt 区别

原文:http://www.cnblogs.com/zhengming2016/p/6992109.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!