数据类型转换
其他类型转为字符串(string)
1. x.toString()

console.log理论上只能接受字符串:
console.log(1)
将转换成console.log( (1).toString() )
2. String(x)

3. 简便方法: x + ''

> 1+'1'
< '11'
等价于(1).toString()+'1'
其他类型转为数值(number)
- Number(x)
- parseInt(x,10), 10为转为十进制,MDN详细介绍
- parseFloat(x),MDN详细介绍
- x - 0
- + x
方法4最常用;方法2面试题常考。
其他类型转为布尔(Boolean)
1. Boolean(x)
2. !!x
当转为布尔值时,共有五个falsy值,分别为: