number、 string、 boolean、null、undefined、symbol、bigint
对象类型 object、 函数类型 function
Number([value])
来完成的。parseInt \ parseFloat
parseInt(""); // NaN
Number(""); // 0
isNaN(""); // isNaN(Number(""))=>isNaN(0) false
parseInt(null); // parseInt('null') NaN
Number(null); // 0
isNaN(null); // false
parseInt('12px'); // 12
Number("12px"); // NaN
isNaN("12px"); // true
parseFloat("1.6px")+parseInt("1.2px")+typeof parseInt(null);
// 1.6+1+'number' '2.6number'
isNaN(Number(!!Number(parseInt('0.8'))));
// isNaN(Number(!!Number(0))) isNaN(Number(false)) isNaN(0) false
typeof !parseInt(null) + !isNaN(null);
// 'boolean'+true 'booleantrue'
+
在JS中很特殊:可以是数学运算 1+1
,也可以是字符串拼接 '1' + '1'
+
的左右两边 出现字符串 或者对象 时候,很多情况下是字符串拼接valueOf
或者 toString
方法,会先转为 字符串,再进而转为数字。但是当转为字符串的时候,就会进行字符串拼接了。很不好意思,它会截胡。
let result = 10 + false + undefined + [] + 'Tencent' + null + true + {}; 10 + false => 10 + 0; 10 + undefined => 10 + NaN NaN + [] => NaN + [].toString() => NaN + "" => 'NaN'; 后续的就变为了字符串拼接 // ==> 'NaNTencentnulltrue[object Object]' let str = 100 + true + 21.2 + null +undefined + 'Tencent' + [] + null + 9 + false; 100 + true + 21.2 + null; // 122.2 // 无字符串和对象,true =》 1 null =》 0 122.2 + undefined; // NaN // undefined => NaN NaN + [number] = NaN NaN + ‘Tencent’ // 'NaNTencent' 进行字符串拼接 'NaNTencent' + [] // 'NaNTencent' // [] => "" 'NaNTencent' + null + 9 + false; // 'NaNTencentnull9false';{}+0?alert('ok'): alert('no'); // no 0+{}?alert('ok'): alert('no'); // ok {}+0, 认为是 代码块{} +0,代码块没有意义,所以就是0 0+{},会是拼接,'0[object Object]'
typeof
instanceof
检测是否为某个类的实例constructor
检测构造函数object.prototype.toString.call([value])
完整版检测数据类型。原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有