isNaN和Number.isNaN
Both of these are used to determine whether the parameter is of type NaN.
The implementation principle of isNaN is: through the Number() method, try to convert the parameter into a Number type . If it succeeds, it returns false, and if it fails, it returns true.
isNaN just judges whether the incoming parameter can be converted into a number, not strictly judges whether the parameter is equal to NaN .
And Number.isNaN() will judge whether the incoming parameter is a number type (NaN is also a special number type) and whether it is strictly equal to NaN (===).
console.log(isNaN('123')) //fasle,字符串类型的123能转成数字,所以返回false
console.log(Number.isNaN('123')) //false,字符串类型的123不是NaN,所以返回false
console.log(Number.isNaN(123) // false
console.log(isNaN('NaN')) // true
console.log(isNaN('nAn')) // true
console.log(isNaN('测试')) //true 因为Number(字符串)会返回NaN
console.log(Number.isNaN('测试')) //false,字符串不是NaN,所以返回false
console.log(Number.isNaN(NaN)) //true
isFinite、Number.isFinite
Both methods are used to determine whether the parameter is finite or not.
When the parameter is positive infinity ( Number.POSITIVE_INFINITY and Infinity ), negative infinity ( Number.POSITIVE_INFINITY and -Infinity ), NaN. return false
The difference between isFinite and Number.isFinite is the same as above, and the default parameter of Number.isFinite is a number type. And IsFinite will convert the parameter to a numeric type before judging.
For the Number.isFinite() method. The following two points must be met to return true.
The parameter is numeric,
Argument is not infinite/small or NaN
console.log(Number.isFinite('123'))// false
console.log(Number.isFinite('abc')) //fasle
console.log(Number.isFinite(NaN)) // false
console.log(Number.isFinite(1)) // true
console.log(isFinite('123')) //true
console.log(isFinite('abc')) // fasle 因为Number('abc')是NaN
console.log(isFinite(NaN)) // false 同上
console.log(isFinite(-Infinity )) // false 因为Number(-Infinity)为-Infinity
Notice
Number(null) // 0
Number('') // 0
console.log(isFinite(null )) //true