Data type conversion
Other types into a string (string)
1. x.toString()
console.log string theory can only accept:
console.log(1)
will be converted intoconsole.log( (1).toString() )
2. String(x)
3. Simple Method: x + ''
> 1+'1'
< '11'
Equivalent to(1).toString()+'1'
Other types into the value (number)
- Number(x)
- parseInt (x, 10), 10 is converted to decimal, the MDN details
- parseFloat (X), the MDN details
- x - 0
- + x
The method most commonly used; interview questions often test method.
Other types into Boolean (Boolean)
Boolean 1. (the X-)
2. the X-!!
When converted to a Boolean value, a total of five falsy value, namely:
- O
- NaN
- ''
- null
- undefined
Reproduced in: https: //juejin.im/post/5d060078f265da1b667bd9b1