数据类型转换
其他类型转为字符串(string)
1. x.toString()
image
console.log理论上只能接受字符串:
console.log(1)
将转换成console.log( (1).toString() )
2. String(x)
image
3. 简便方法: x + ''
image
> 1+'1'
< '11'
等价于(1).toString()+'1'
其他类型转为数值(number)
方法4最常用;方法2面试题常考。
其他类型转为布尔(Boolean)
1. Boolean(x)
2. !!x
当转为布尔值时,共有五个falsy值,分别为:
- o
- NaN
- ''
- null
- undefined