Number和ParseInt的区别
Number:除了数字、数字型字符串、null、false、"",其它都转换成NaN;
ParseInt:除了数字和带单位的数字,其它都转换成NaN;
console.log(Number("123"));//123
console.log(Number(123));//123
console.log(Number(null));//0
console.log(Number(false));//0
console.log(Number(""));//0
console.log(Number("123px"));//NaN
console.log(Number("123"));//123
console.log(parseInt("123px"));//123
console.log(parseInt(null));//NaN
console.log(parseInt(false));//NaN
console.log(parseInt(""));//NaN
Boolean类型的转换规则
-
String:空字符串为false,非空字符串为true
console.log(Boolean(""));//false console.log(Boolean("1"));//true
-
Number:0和NaN为false,非零数值为true
console.log(Boolean(0));//false console.log(Boolean(NaN));//false console.log(Boolean(1));//true console.log(Boolean(2));//true
-
Object:null为false,不为null为true
console.log(Boolean(null));//false console.log(Boolean({}));//true
-
Undefine:undefin为false
console.log(Boolean(undefined));//false