<script type="text/javascript">
/*
将其他数据类型转为 String
*
*
* 方式1:
* 调用被转换数据类型toString()方法
* 此方法不会影响到原变量,将转换的结果返回
* 注意:null与undefined,这两个值没有toString(),报错
* 方式2:
* 调用String()函数 并将转换为的数据作为参数传递给函数
* 对于 number,boolean,转换使用toSring()方法
* 如果想转换 null或者undefined 则使用String()函数
* 将 null 转为 "null"
* 将undefined 转为 "undefined"
* */
//将number转为string
var a = 123;
//将boolean转为String
a = true;
//将null转为string
// a = null;//报错
//将undefined转为string
// a = undefined;//报错
console.log(a);
console.log(a.toString());
console.log(typeof a.toString());
//调用String()将a转为字符串
a = null;
a = String(a)
console.log(a)
console.log(typeof a)
//将undefined转为string
a = undefined;
a = String(a)
console.log(a)
console.log(typeof a)
</script>