toString()
<script type="text/javascript"> var num = 5; console.log(typeof num.toString()); </script>
String()
var sz = null; console.log(sz.toString()); console.log(String(sz)); /* String()函数存在的意义;有些值没有toString(); 这个时候可以使用String(),比如undefined和null; */
随堂笔记
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script type="text/javascript"> var n = 5; var s = n.toString(); console.log(typeof s); console.log(typeof String(n)); var num = ''+n; console.log(typeof num); var num = true; console.log(typeof num.toString()); </script> </body> </html>
转载请注明:大灰牛博客 » JavaScript其他类型转为字符串