应用
let a = 1
let b= 123
String(a) // '1'
typeOf a // String
b.toString() // '123'
typeOf b // String
区别
- toString()无法转换nudefined、null
let a
let b = null
a.toString() // Uncaught TypeError: Cannot read property 'toString' of undefined
b.toString() // Uncaught TypeError: Cannot read property 'toString' of undefined
String(a) // 'undefined'
String(b) // 'null'