首页 > Web开发 > 详细

JS中数据类型的转换

时间:2019-06-26 23:29:41      阅读:187      评论:0      收藏:0      [点我收藏+]

转换为数字类型 Number

字符串转数字类型

Number(‘1‘)===1;
parseInt(‘1‘,10)===1;
parseFloat(‘1.1‘)===1.1;
X -0
+ X

转换为字符串类型

String

String(1);//"1"
String(true);//"true"
String(null);//"null"
String(undefined);//"1"
String({});//"[object Object]"

toString

(1).toString();//"1"
true.toString();//"true"

null.toString();//报错
//Uncaught TypeError: Cannot read property ‘toString‘ of null

undefined.toString();//报错
//Uncaught TypeError: Cannot read property ‘toString‘ of undefined

{}.toString();//报错
//Uncaught SyntaxError: Unexpected token .

[{}].toString();//"[object Object]"

+ ‘‘

1+‘‘ //"1"
true+‘‘ //"true"
null+""//"null"
undefined+‘‘ //"undefined"
{}+‘‘ //0

var e={};
e+‘‘;//"[object Object]"

转布尔类型

Boolean(x)
Boolean(‘‘)//false
Boolean({})//true

!!x

五个falsy值:

0

NaN

null

undefined

在布尔上下文中认定可转为false的值

JS中数据类型的转换

原文:https://www.cnblogs.com/BUBU-Sourire/p/11094503.html

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