强制转换
转换成String
1、ToString
var num = 1;
var boo1 = false;
var arr = [1, 2, 3];
var obj = {};
var fn = function () {console.log("我是fn")};
var un1 = undefined;
var nu1 = null;
console.log(num.toString()); // 1
console.log(boo1.toString()); // false
console.log(arr.toString()); // 1,2,3
console.log(obj.toString()); // [object Object]
console.log(fn.toString()); // function () {console.log("我是fn")}
console.log(un1.toString()); // Uncaught TypeError: Cannot read property 'toString' of undefined
console.log(nu1.toString()); // Uncaught TypeError: Cannot read property 'toString' of null
null和undefined这两个值没有toString()方法,如果调用他们的方法,会报错
2、String
var num = 1;
var boo1 = false;
var arr = [1, 2, 3];
var obj = {};
var fn = function () {console.log("我是fn")};
var un1 = undefined;
var nu1 = null;
console.log(String(num)); // 1
console.log(String(boo1)); // false
console.log(String(arr)); // 1,2,3
console.log(String(obj)); // [object Object]
console.log(String(fn)); // function () {console.log("我是fn")}
console.log(String(un1)); // Uncaught TypeError: Cannot read property 'toString' of undefined
console.log(String(nu1)); // Uncaught TypeError: Cannot read property 'toString' of null
使用String()函数做强制类型转换时,对于Number和Boolean实际上就是调用的toString()方法,但是对于null和undefined,就不会调用toString()方法。它会将 null 直接转换为 "null",将 undefined 直接转换为 "undefined"
转换成数字
Number
var un = undefined;
var nu = null;
var str1 = '';
var str2 = '34';
var str3 = '34px';
var str4 = 'hello';
var boo1 = false;
var boo2 = true;
var arr1 = [];
var arr2 = [34];
var arr3 = [34, 55];
var obj1 = {}
var fn = function(){return 21}
var date = new Date()
console.log(Number(un)) // Nan
console.log(Number(nu)) // 0
console.log(Number(str1)) // 0
console.log(Number(str2)) // 34
console.log(Number(str3)) // NAN
console.log(Number(str4)) // NAN
console.log(Number(boo1)) // 0
console.log(Number(boo2)) // 1
console.log(Number(arr1)) // 0
console.log(Number(arr2)) // 34
console.log(Number(arr3)) // NAN
console.log(Number(obj1)) // NAN
console.log(Number(fn)) // NAN
console.log(Number(date)) // 1554639507784
parseInt
var un = undefined;
var nu = null;
var str1 = '';
var str2 = '34';
var str3 = '34px';
var str4 = 'hello';
var num1 = 22.22;
var boo1 = false;
var boo2 = true;
var arr1 = [];
var arr2 = [34];
var arr3 = [34, 55];
var obj1 = {}
var fn = function(){return 21}
var date = new Date()
console.log(parseInt(un)) // Nan
console.log(parseInt(nu)) // NAN
console.log(parseInt(str1)) // NAN
console.log(parseInt(str2)) // 34
console.log(parseInt(str3)) // 34
console.log(parseInt(str4)) // NAN
console.log(parseInt(num1)) // 22
console.log(parseInt(boo1)) // NAN
console.log(parseInt(boo2)) // NAN
console.log(parseInt(arr1)) // NAN
console.log(parseInt(arr2)) // 34
console.log(parseInt(arr3)) // 34
console.log(parseInt(obj1)) // NAN
console.log(parseInt(fn)) // NAN
console.log(parseInt(date)) // NAN
parseFloat
var un = undefined;
var nu = null;
var str1 = '';
var str2 = '34';
var str3 = '34px';
var str4 = 'hello';
var num1 = 22.22;
var boo1 = false;
var boo2 = true;
var arr1 = [];
var arr2 = [34];
var arr3 = [34, 55];
var obj1 = {}
var fn = function(){return 21}
var date = new Date()
console.log(parseFloat(un)) // Nan
console.log(parseFloat(nu)) // NAN
console.log(parseFloat(str1)) // NAN
console.log(parseFloat(str2)) // 34
console.log(parseFloat(str3)) // 34
console.log(parseFloat(str4)) // NAN
console.log(parseFloat(num1)) // 22.22
console.log(parseFloat(boo1)) // NAN
console.log(parseFloat(boo2)) // NAN
console.log(parseFloat(arr1)) // NAN
console.log(parseFloat(arr2)) // 34
console.log(parseFloat(arr3)) // 34
console.log(parseFloat(obj1)) // NAN
console.log(parseFloat(fn)) // NAN
console.log(parseFloat(date)) // NAN
隐式转换
转换成字符串
+号运算符
1 转换规则
- 如果两个操作值都是字符串,则将它们拼接起来
- 如果只有一个操作值为字符串,则将另外操作值转换为字符串,然后拼接起来
- 如果一个操作数是对象、数值或者布尔值,则调用toString()方法取得字符串值,然后再应用前面的字符串规则。对于undefined和null,分别调用String()显式转换为字符串。
var str1 = '1' + undefined
var str2 = '1' + null
var str3 = '1' + '1'
var str4 = '1' + true
var str5 = '1' + 1
var str6 = '1' + {}
var str7 = '1' + []
var str8 = '1' + [1]
var str9 = '1' + [1, 2]
var str10 = '1' + function(){console.log(1)}
var str11 = 1 + {}
var str12 = 1 + []
var str13 = 1 + [1]
var str14 = 1 + [1, 2]
var str15 = 1 + function(){console.log(1)}
console.log(str6, typeof str6) // 1[object Object] string
console.log(str7, typeof str7) // 1 string
console.log(str8, typeof str8) // 11 string
console.log(str9, typeof str9) // 11,2 string
console.log(str10, typeof str10) // 1function(){console.log(1)} string
console.log(str1, typeof str1) // 1undefined string
console.log(str2, typeof str2) // 1null string
console.log(str3, typeof str3) // 11 string
console.log(str4, typeof str4) // 1true string
console.log(str5, typeof str5) // 11 string
console.log(str6, typeof str6) // 1[object Object] string
console.log(str7, typeof str7) // 1 string
console.log(str8, typeof str8) // 11 string
console.log(str9, typeof str9) // 11,2 string
console.log(str10, typeof str10) // 1function(){console.log(1)} string
console.log(str11, typeof str11) // 1[object Object] string
console.log(str12, typeof str12) // 1 string
console.log(str13, typeof str13) // 11 string
console.log(str14, typeof str14) // 11,2 string
console.log(str15, typeof str15) // 1function(){console.log(1)} string
转换成数字
+号运算符
1 转换规则
如果两个操作值都是数值
- 如果一个操作数为NaN,则结果为NaN
- 如果是Infinity+Infinity,结果是Infinity
- 如果是-Infinity+(-Infinity),结果是-Infinity
- 如果是Infinity+(-Infinity),结果是NaN
- 如果是+0+(+0),结果为+0
- 如果是(-0)+(-0),结果为-0
- 如果是(+0)+(-0),结果为+0
var num1 = 1 + undefined
var num2 = 1 + null
var num3 = 1 + '1'
var num4 = 1 + true
var num5 = 1 + 1
var num6 = 1 + NaN
var num7 = Infinity + Infinity
var num8 = -Infinity + -Infinity
var num9 = Infinity + -Infinity
var num10 = 0 + 0
var num11 = -0 + -0
var num12 = 0 + -0
console.log(num1, typeof num1) // NaN "number"
console.log(num2, typeof num2) // 1 "number"
console.log(num3, typeof num3) // 11 string
console.log(num4, typeof num4) // 2 "number"
console.log(num5, typeof num5) // 2 "number"
console.log(num6, typeof num6) // NaN "number"
console.log(num7, typeof num7) // Infinity "number"
console.log(num8, typeof num8) // -Infinity "number"
console.log(num9, typeof num9) // NaN "number"
console.log(num10, typeof num10) // 0 "number"
console.log(num11, typeof num11) // -0 "number"
console.log(num12, typeof num12) // 0 "number"
乘除、减号运算符、取模运算符
这些操作符针对的是运算,所以他们具有共同性:如果操作值之一不是数值,则被隐式调用Number()函数进行转换。
转换成布尔值
1、转换规则
1.1 若类型相同
1.2 若类型不相同
- undefined、null相等
- boolean、string、number之间相比,会转换成Nunber
- boolean、string、number和对象相比,对象会转换成基本类型,再进行对比
2、转换成布尔值的运算符类型
- if (..) 语句中的条件判断表达式。
- for ( .. ; .. ; .. ) 语句中的条件判断表达式(第二个)。
- while (..) 和 do..while(..) 循环中的条件判断表达式
- ? : 中的条件判断表达式。
- 逻辑运算符 || (逻辑或)和 && (逻辑与)左边的操作数(作为条件判断表达式)
- 宽松相等和严格相等:“ == 允许在相等比较中进行强制类型转换,而 === 不允许。