一、 强制转换
使用String(),Number(),Boolean()函数强制转换
var str=100;
var str1='100';
console.log(typeof str); //number
console.log(typeof str1); //string
console.log(typeof String(str)); //string
console.log(typeof Number(str1)); //number
二、隐式转换
1.字符串加数字,数字就会转成字符串。
2.数字减字符串,字符串转成数字。如果字符串不是纯数字就会转成NaN。字符串减数字也一样。两个字符串相减也先转成数字。
3.乘,除,大于,小于跟减的转换也是一样。
console.log(1+'2') //12 string
console.log(1-'2')//-1 number
console.log(10-'df') //NaN
console.log(10-'10a') //NaN
console.log(10*'20') //200 number
console.log('10'*'20') //200 number
console.log(2/'1') //2 number
console.log('2'/'1') //2 number
console.log('2'/'ger') //NaN
4. ==时候也会进行隐式转换()
1.undefined等于null
2.字符串和数字比较时,字符串转数字
3.数字为布尔比较时,布尔转数字
4.字符串和布尔比较时,两者转数字
console.log(undefined==null) //true
console.log('0'==0) //true 字符串转数字
console.log(0==false) //true 布尔转数字
console.log('0'==false) //2个都转成数字
console.log(null==false) //false
console.log(undefined==false)//false