Number和ParseInt的区别以及Boolean类型的转换规则

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类型的转换规则

  1. String:空字符串为false,非空字符串为true

    		console.log(Boolean(""));//false
            console.log(Boolean("1"));//true
    
  2. 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
    
  3. Object:null为false,不为null为true

    		console.log(Boolean(null));//false
            console.log(Boolean({}));//true
    
  4. Undefine:undefin为false

    		console.log(Boolean(undefined));//false
    
上一篇:SQL语句在查询分析器中可以执行,代码中不能执行


下一篇:HTML+CSS+JS简单的方框移动