<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
</head>
<body>
<script>
//1.数字与字符串之间的转换?
//number--->string
num1=100;
str=num1.toString()
alert(typeof str)//string
//string--->number
str1="112";
var num1=Number.parseInt(str1)
//var num1=Number.parseFloat(str1)
alert(typeof num1);//number
//2.数字与boolean之间的转换?
//number--->boolean
num2=0;
num3=Number.parseInt(10);
bool = Boolean(num2);//非0是true
bool2= Boolean(num3);
alert(typeof bool)//false boolean
alert( bool2)//true bloolean
//boolean--->number
bool3 = Boolean(100);
//alert(typeof bool3);//boolean
var num4 = Number.parseInt(new Number(bool3));
alert(typeof num4)//number 1
/*拆解后
var numobj = new Number(bool3);
alert( numobj)//1 object 当布尔值为非0是转换为number对象时值为1
var num4 = Number.parseInt(numobj);
alert(typeof num4)//1 number
*/
//3.字符串与boolean之间的转换?空字符串是false
//string--->boolean
str = "fxt";
str2="";
bool4= Boolean(str);
bool5= Boolean(str2);
alert(typeof bool4);//true blooean
alert( bool5);//false blooean
//boolean--->string
bool6 = Boolean("ftx");
alert( bool6)//boolean true
var str3 = bool6.toString();
alert(typeof str3)//string 返回结果是true不是fxt
</script>
</body>
</html>
05-28
309
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交