The article was originally posted on my personal blog.
I was recently answering an interesting question on Stack Overflow about the type of
this is what´s happening
typeof Number.parseInt('processed') prints
And indeed if you type in your browser console
In Ruby, for example
NaN can be either an instance of
And indeed, if we look at the ECMA-262, 10th edition, the ECMAScript Language Specification, section 4.3.21 states that number type represents a "set of all possible Number values including the special “Not-a-Number” (NaN) value, positive infinity, and negative infinity". A few subsections below, section 4.3.24 clarifies that NaN is a "number value that is an IEEE 754-2008 “Not-a-Number” value".
First published in 1985, its main purpose is to provide a computational method with floating point numbers, which would have the same results independent of the environment where the processing is done, be it software, hardware or a mix of both. Together with specifying formats and methods for floating-point arithmetic in computer programming environments, IEEE 754 also defines a set of special values:
+0 are distinct values, although they both are equal; here's an in-depth article about both zero values in JS), denormalised number, positive and negative
NaN, which the standard describes as a numeric data type that cannot be represented within the computing system. In fact, IEEE 754 defines two types of
NaN - a quiet
qNaN) and a signalling
sNaN). The most important difference between the two is that
sNaN will cause an exception when used in arithmetic operations and
NaNs are quiet, at least I wasn't able to find any information to the contrary.
Additionally, the standard defines an interesting list of special operations and their results:
number ÷ Infinity = 0 number ÷ -Infinity = -0 ±Infinity × ±Infinity = ±Infinity ±non zero number ÷ ±0 = ±Infinity number × ±Infinity = ±Infinity Infinity + Infinity = ±Infinity Infinity – -Infinity = +Infinity -Infinity – Infinity = Infinity -Infinity + – Infinity = Infinity ±0 ÷ ±0 = NaN ±Infinity ÷ ±Infinity = NaN ±Infinity × 0 = NaN NaN == NaN (also '===' in JS) //False
NaN value, in fact, according to ECMA-262, there are "9007199254740990 (that is, 253 - 2) distinct “Not-a-Number” values of the IEEE Standard [...] represented in ECMAScript as a single special NaN value. In some implementations, external code might be able to detect a difference between various Not-a-Number values, but such behaviour is implementation-dependent; to ECMAScript code, all NaN values are indistinguishable from each other."
NaN doesn't equal to itself (and in fact in some programming languages self-comparison is a widely used approach to test for
NaNs), we need to use a special
isNaN() methods to detect
NaNs. The difference between the two is that
true if the value is currently
NaN or it is going to be NaN after it's coerced to numeric value and
Number.isNaN() only will return
true if the value is currently
Number.isNaN() is more accurate since it also checks if the type of value is number. MDN has a handy explanation about testing against NaN.
Got any questions/comments or other kinds of feedback about this post? Let me know here in the comments or on Twitter.