类型转换
其他类型转数字类型:三种方式
1. parseInt()------转整数
console.log(parseInt("10"));//10 console.log(parseInt("10afrswfdsf"));//10 console.log(parseInt("g10"));//NaN console.log(parseInt("1fds0"));//1 console.log(parseInt("10.98"));//10 console.log(parseInt("10.98fdsfd"));//10
2. parseFloat()------转小数
console.log(parseFloat("10"));//10 console.log(parseFloat("10afrswfdsf"));//10 console.log(parseFloat("g10"));//NaN console.log(parseFloat("1fds0"));//1 console.log(parseFloat("10.98"));//10.98 console.log(parseFloat("10.98fdsfd"));//10.98
3. Number()-------转数字
console.log(Number("10"));//10 console.log(Number("10afrswfdsf"));//NaN console.log(Number("g10"));//NaN console.log(Number("1fds0"));//NaN console.log(Number("10.98"));//10.98 console.log(Number("10.98fdsfd"));//NaN
总结:转整数用parseInt() , 想要转小数用parseFloat() ,想要转数字用Number() ----比前面两种方式严格。
其他类型转字符串类型:
1. 变量名.toString()
2. String(变量名)
注意: 如果变量有意义调用.toString()转换 变量没有意思使用String()转换
其他类型转布尔类型:
1. Boolean(值)
console.log(Boolean(1));//true console.log(Boolean(0));//false console.log(Boolean(11));//true console.log(Boolean(-10));//true console.log(Boolean("哈哈"));//true console.log(Boolean(""));//false console.log(Boolean(null));//false console.log(Boolean(undefined));//false