The data type conversion JS

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)

  1. Number(x)
  2. parseInt (x, 10), 10 is converted to decimal, the MDN details
  3. parseFloat (X), the MDN details
  4. x - 0
  5. + 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

Guess you like

Origin blog.csdn.net/weixin_34092370/article/details/93180532