week1.2数据类型转换
强制类型转换
转换为string
String()函数
可以将任何数据转换为string类型
String(10);// '10'
String(NaN);// 'NaN'
String(null);// 'null'
String(undefined);// 'undefined'
String(true);// 'true'
toString()函数
可以将除了null/undefined之外的任何数据转换为string类型
var num = 10;
num.toString();// '10'
var flag = true;
flag.toString();// 'true'
var z = undefined;
var o = null;
console.log(typeof z.toString()); //报错 z.toString()方法,值不能为undefined和null
console.log(typeof o.toString()); //报错
console.log( typeof String(z));//string类型
console.log( typeof String(o));//string类型
区别
区别1:
String()可以将任何数据转换为string类型
toString() 可以将除了null/undefined之外的任何数据转换为String类型
区别2:
String() 将需要被转换的内容放入小括号内部; String(num);
toString()直接调用变量的toString()方法; num.toString()
转换为number
Number()
如果内容可以转换成数字,则返回对应的数字(整数或小数)
如果内容不可以转换成数字,则返回NaN
如果内容为空,则返回0
var a = 10;
var b = "hello";
var bb = "18hello"
var bbb = "18"
var bbbb = "hello18"
var c = true;//注意
var d = undefined ;//注意
var e = null; //注意
var f = 10.99;
console.log(Number(a) ); //10
console.log(Number(b) ); //NaN
console.log(Number(bb) ); // NaN
console.log(Number(bbb) ); //18
console.log(Number(bbbb) );//NaN
console.log(Number(c) );// TRUE,为真相当于1
console.log(Number(d) ); //NaN
console.log(Number(e) );// NULL,为假相当于0
console.log(Number(f) );//10.99
parseInt()
逐个解析,转换成整数,遇到小数点直接砍掉
console.log(parseInt(a)); //10-----10
console.log(parseInt(f));//10-----10
console.log(parseInt(bb));//18hello---18
console.log(parseInt(bbbb));//hello18---NaN
parseFloat()
解析带上小数点
console.log(parseFloat(f));//10.99
区别
parseInt和parseFloat解析会从第一字符进行解析,直到遇到不能被解析的字符为止,返回已经被解析的内容。
Number和parseFloat都可以识别小数,parseInt不可以。
parseFloat和parseInt都可以识别出开头是数字的字符串。
转换为boolean
false、0、NaN、Undefined、null、空字符串会被转换成false
其它的都会被转成true(任何非空字符串都会被转换为true)
注意:Boolean值在内存中true为1,false为0
var a = false;
var b = "";
var c = NaN;
var d = undefined;
var e = null;
var f = 0;
var A = " ";//空格
var B = "false";
console.log(Boolean(a));//false
console.log(Boolean(b));//false
console.log(Boolean(c));//false
console.log(Boolean(d));//false
console.log(Boolean(e));//false
console.log(Boolean(f));//false
console.log(Boolean(A));//" "true
console.log(Boolean(B));//" "true
自动类型转换(隐式转换)
转换为string
任何数据与字符串相加都是字符串的拼接操作,得到的结果也都是字符串。
var a = false;
var b = "";
var c = NaN;
var d = undefined;
var e = null;
var f = 0;
var A = " ";//空格
var B = "false";
console.log (a + "10");//false10
console.log (b + "10");// 10
console.log (c + "10");//NaN10
console.log (d + "10");//undefined10
console.log (e + "10");//null10
console.log (f + "10");//010
转换为number
直接在要转换的内容前加上”+”;
可以使用-、*、/、%、>、<等运算符将字符串转换成number
//+转换成数字类型
console.log(+"10"); //10 ,
console.log(+"hello"); //NaN
console.log(+"0");//0
console.log("10" +5);//105
console.log("10"- 5);//5
console.log("10" *5);//50
console.log("10"/ 5);//2
console.log("10" %5);//0
console.log("10">5);//true
console.log("10" <5);//false
console.log("10"> "5");//false 因为1<5
console.log("a">"B");//true a 97 >B66,和字符编码有关系
转换为boolean
对于boolean类型的自动转换有很多操作可以实现,暂时我们学习过的有:逻辑运算符 ! 的操作。
if 和 while 也可以。
凡是需要写条件的,都是隐式转换布尔。
!12;// false
!1;// false
!'hello';// false
!!'hello';// true
!!123;// true