假设我想转换一个显然不是数字的字符串,但是在最后我会得到一个数字!?
var b = parseFloat("a");
console.log(b); // output NaN
console.log(typeof b); // print number!
这到底发生了什么? NaN意味着不是数字,对吧?
好的,所以NaN == NaN总是假的。但是:
typeof 1/0 // prints NaN
1/0 == 1/0 // its true
答案 0 :(得分:1)
它并不特定于Javascript。它与计算机科学更相关。
在计算中,NaN(不是数字)是表示未定义或不可表示值的数值数据类型值,尤其是在浮点计算中。
有三种操作可以返回NaN
Operations with a NaN as at least one operand.
Indeterminate forms
The divisions 0/0 and ±∞/±∞
The multiplications 0×±∞ and ±∞×0
The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions
The standard has alternative functions for powers:
The standard pow function and the integer exponent pown function define 00, 1∞, and ∞0 as 1.
The powr function defines all three indeterminate forms as invalid operations and so returns NaN.
Real operations with complex results, for example:
The square root of a negative number.
The logarithm of a negative number
The inverse sine or cosine of a number that is less than −1 or greater than +1.
答案 1 :(得分:1)
var b = parseFloat("a");
console.log(b); // output NaN
console.log(typeof b); // number! what else would it be?
console.log(!isNaN(b)); // false
console.log(isNaN(b)); // true!
console.log(isNaN('stringoftext')); // true! this is not good, that's REALLY not a number
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN
console.log(typeof b === 'number' && isNaN(b) === true); // It is NOT A NUMBER
console.log(b != b); // true , means that it is NaN
//http://stackoverflow.com/a/14220688/3741423
答案 2 :(得分:0)
Threre不是你想要的解释。
功能parseFloat
可能会引发异常,但这是另一种情况。
它返回特殊的"数字"叫"不是号码"。
只是不要带着这个名字循环。