NaN
常用
NaN === NaN // false 跟谁都不相等,比较特殊
//
parseInt parseFloat
他俩遇到非有效数字,就会返回 NaN, 对于传经来的值不是字符串类型,会先转换为字符串类型,然后在开始从左往右 找
在这里插入代码片
parseInt('123') // 123
parseInt('a12') // NaN
parseInt(null) // NaN 与Number 执行的 底层机制不一样
parseInt(undefined) // NaN
parseInt('') // NaN
parseFloat(12.36) // 12.36 会转换 小数点
parseFloat('as123') // NaN
在 Number 中转换时 ,遇到非有效数字,(空对象、正则、函数)也会变成 NaN
空数组 [] 会默认调用 toString 转给 空字符串, 所以最后还是 0
在这里插入代码片
Number('adf') // NaN
Number(undefined) // NaN
Number(null) // 0
Number(true) // 1
Number(false) // 0
Number({}) // NaN
Number([]) // 0
Number([12]) // 12
Number([12,12]) // NaN
Number((NaN) // NaN
Boolean
除了与 空字符串‘’ 、null 、 undefined 、 NaN 、 0 这 5种 是 false 其他转换都是 true
在这里插入代码片
Boolean('') // false
Boolean(null) // false
Boolean(undefined) // false
Boolean(NaN) // false
Boolean(0) // false
isNaN
在这里插入代码片
isNaN(null) // false 先调用底层 Number 方法转为 0
isNaN(NaN) // true
面试题
parseInt('') // false
Number('') // 0
parseInt(null) // NaN
Number(null) // 0
isNaN(null) // false
parseInt('12px') // 12
Number('12px') // NaN
isNaN('12px') // true
变态面试题
在这里插入代码片
!(!"Number(undefined)") // true
isNaN(parseInt(new Date())) + Number([1]) + typeof undefined // "2undefined"
Boolean(Number("")) + !isNaN(Number(null)) + Boolean(("parseInt([])") + typeof !((null) // "2boolean"
parseFloat("1.6px") + parseInt("1.2px") + typeof parseInt(null) // "2.6number"
isNaN(Number(!!Number(parseInt("0.8")))) // false
console.log(1+"2"+"3") // 123
!typeof parseFloat("0") // false
Number("") // 0
typeof "parseInt(null)" + 12 + !!Number(NaN) // "string12false"
typeof !parseInt(null) + !isNaN(null) // "booleantrue"