法一:调用toString()方法函数
但null和undefined这两个值没有toString()方法spa
法二:调用String()函数字符串
注: 调用xxx的yyy()方法,就是xxx.yyy();string
调用xxx的yyy函数,就是xxx();console
法一:调用Number()函数变量
一、字符串转为数字;object
a、若是是纯数字的字符串,则直接将其转换为数字;数据类型
b、若是字符串中有非数字的内容,则转换为NaN;方法
c、若是字符串是一个空串或一个全为空格的字符串,则转化为0;数据
二、布尔值转为数字;
a、true转为1;
b、false转为0;
三、null转为0;
四、undefined转为NaN;
法二:只针对字符串
一、parseInt() 把一个字符串转换为一个整数;
二、parseFloat()把一个字符串转换为一个浮点数;
调用Boolean()函数
一、数字转为布尔;
除了0和NaN,其他的都是true;
二、字符串转为布尔;
除了空串,其他都是true;(空格也是true)
三、null、undefined、object都会转为false;
console.log(typeof a); //'undefined'
console.log(typeof(true)); //'boolean'
console.log(typeof '123'); //'string'
console.log(typeof 123); //'number'
console.log(typeof NaN); //'number'
console.log(typeof defined); //'number'
console.log(typeof null); //'object'