空数组[]:
转化为字符串:为空字符串;其他非空数组直接转换为"元素"
转化为number:为0;其他非空数组转换为数值,为NaN
转化为Bollean:为true
<script>
var num1 = [];
var num2 = [1, 2];
// 返回此对象
console.log(num1.toString());//空
console.log(typeof (num1.toString()));
console.log(num2.toString());//[1,2]
console.log('--------------------');
//转为字符串
console.log(String(num1));//[空]
console.log(typeof (num1.toString()));
console.log(String(num2));//[1,2]
console.log('--------------------');
//转为Number
console.log(Number(num1));//0
console.log(Number(num2));//NaN
console.log('--------------------');
// 转为Boolean
console.log(Boolean(num1)); //true
console.log(Boolean(num2)); //true
</script>
空对象{}:
转化为字符串:为Object,不管是否为空
转化为number:为NaN,不管是否为空
转化为Bollean:为true
<script>
var obj1 = {};
var obj2 = { name: 'nana' };
// 返回此对象
console.log(obj1.toString());//[object Object]
console.log(obj2.toString());//[object Object]
console.log('--------------------');
//转为字符串
console.log(String(obj1));//[object Object]
console.log(String(obj2));//[object Object]
console.log('--------------------');
//转为Number
console.log(Number(obj1));//NaN
console.log(Number(obj2));//NaN
console.log('--------------------');
// 转为Boolean
console.log(Boolean(obj1)); //true
console.log(Boolean(obj2)); //true
</script>