显示转换
1.将其他类型转换为string类型
1.使用字符串拼接
var a = 10;
var b = true;
console.log(a+"",typeof(a+""));//undefined string
console.log(b+"",typeof(b+""));//undefined string
console.log(null+"",typeof(null+""));//null string
console.log(undefined+"",typeof(undefined+""));//undefined string
2.使用toString()函数方法
null和undefined不可以调用toString() 没有包装器
var a = 10 ;
var b = true;
console.log(a.toString(),typeof(a.toString()));//10 string
console.log(b.toString(),typeof(b.toString()));//true string
console.log(null.toString(),typeof(null.toString()));//报错
console.log(undefined.toString(),typeof(undefined.toString()));//报错
3.使用String()包装器函数
var a = 10 ;
var b = true;
console.log(String(a),typeof String(a));//10 string
console.log(String(b),typeof String(b));//true string
console.log(String(null),typeof String(null));//null string
console.log(String(undefined),typeof String(undefined));//undefined ftring
将number转为string 调用toString()可以放入一些进制参数
var a = 10;
console.log(a.toString());
console.log(a.toString(2));//将number转为2进制string
console.log(a.toString(8));//将number转为8进制string
console.log(a.toString(16));//将number转为16进制string
2.将其他数据类型转为number类型
1.使用+
var a ='10';
var b = true;
var c = false;
console.log(+a,+b,+c,typeof(+a),typeof(+b),typeof(+c));//10 1 0 number number number
console.log(+null,+undefined,typeof(+null),typeof(+undefined));//0 NaN number number
2.使用Number包装器函数
var a ='10';
var b = true;
var c = false;
console.log(Number(a),typeof Number(a));//10 number
console.log(Number(b),typeof Number(b));//1 number
console.log(Number(c),typeof Number(c));//0 number
3.使用parseInt()方法
如果转换的值是null,undefined,boolean,均转换为NaN
var a ='10';
var b = true;
var c = false;
console.log(parseInt(a),typeof parseInt(a));//10 number
console.log(parseInt(b),typeof parseInt(b));//NaN number
console.log(parseInt(c),typeof parseInt(c));//NaN number
console.log(parseInt(null),typeof parseInt(null));//NaN number
console.log(parseInt(undefined),typeof parseInt(undefined));//NaN number
4.使用parseFloat()方法
console.log(parseFloat('10'),typeof parseFloat('10'));//10 number
console.log(parseFloat(true),typeof parseFloat(true));//NaN number
console.log(parseFloat(null),typeof parseFloat(null));//NaN number
console.log(parseFloat(undefined),typeof parseFloat(undefined));//NaN number
console.log(parseFloat(""),typeof parseFloat(""));//NaN number
3.将其他数据类型转换为Boolean
1.使用Boolean()包装器函数 true false
console.log(Boolean(10),typeof Boolean(10));//true boolean
console.log(Boolean('hello'),typeof Boolean('hello'));//true boolean
console.log(Boolean(null),typeof Boolean(null));//false boolean
console.log(Boolean(undefined),typeof Boolean(undefined));//false boolean
2.使用!!取反
console.log(!!10,typeof(!!10));//true boolean
console.log(!!'hello',typeof(!!'hello'));//true boolean
console.log(!!null,typeof(!!null));//false boolean
console.log(!!undefined,typeof(!!undefined));//false boolean
隐式转换
隐式转换 +
两个操作数有一个是字符串就是字符串的拼接
隐式转换 - * / % ==
如果两个操作数,一个是数字,一个是字符串,隐式将字符串转换为数字
console.log(1 == '1');//true
如果两个操作数,一个是数字,一个是布尔值,隐式将布尔值转为数字
console.log(1 == true);//true
如果两个操作数,一个是布尔值,一个是字符串,隐式将两者转为数字
console.log(true == '1');//true
引用数据类型隐式转换
console.log([]+[],'转换1');//""
console.log([]+{},'转换2');//[object Object]
console.log({}+{},'转换3');//[object Object] [object Object]
console.log([].toString(),[].valueOf(),typeof([].toString()),typeof([].valueOf()))//"" [] string object
console.log({}.toString(),{}.valueOf(),typeof({}.toString()),typeof({}.valueOf()))//[object Object] {} string object
toString返回值 | valueOf返回值 | toString返回类型 | valueOf返回类型 | |
[] | '' | [] | string | object |
{} | [object Object] | {} | string | object |
[]+[] | '' | '' | string | string |
[]+{} | [object Object] | [object Object] | string | string |
{}+{} | [object Object][object Object] | [object Object][object Object] | string | string |