# The Curious Case of JavaScript NaN

Join the DZone community and get the full member experience.

Join For Free
nan
, not a number, is a special
~~
type
~~
value used to denote an unrepresentable value. with javascript,
```
nan
```

can cause some confusion, starting from its
```
typeof
```

and all to the way the comparison is handled.

several operations can lead to
```
nan
```

as the result. here are some examples (follow along on jsbin:
jsbin.com/yulef
):

math.sqrt(-2) math.log(-1) 0/0 parsefloat('foo')

the first trap for many javascript beginners is usually the unexpected result of calling
```
typeof
```

:

console.log(typeof nan); // 'number'

in a way, while nan isn’t supposed to be a number, its type is number. got it?

stay calm, as this will continue to lead to many confusing paths. let’s compare two nans:

var x = math.sqrt(-2); var y = math.log(-1); console.log(x == y); // false

maybe that’s because we’re supposed to use strict equal (===) operator instead? apparently not.

var x = math.sqrt(-2); var y = math.log(-1); console.log(x === y); // false

arrgh! could it be because they are nans from two different operations? what about…

var x = math.sqrt(-2); var y = math.sqrt(-2); console.log(x == y); // false

even crazier:

var x = math.sqrt(-2); console.log(x == x); // false

what about comparing two real nans?

console.log(nan === nan); // false

because there are many ways to represent a nan, it makes sense that one nan will not be equal to another nan. still, this is the reason why i sometimes tweet:

this is your annual reminder that nan stands for "not a nan".

— ariya hidayat (@ariyahidayat) october 23, 2013

to solve this, originally i intended to submit this proposal for ecmascript 7:

but of course, solutions (and workarounds) already exist today.

let’s get to know the global function isnan :

console.log(isnan(nan)); // true

alas,
```
isnan()
```

has its own well-known flaws:

console.log(isnan('hello')); // true console.log(isnan(['x'])); // true console.log(isnan({})); // true

this often leads to a number of different workarounds. one example is to exploit the non-reflective nature of nan (see e.g. kit cambridge’s note ):

var my = { isnan: function (x) { return x !== x; } }

another example is to check for the value’s type first (to prevent coercion):

my.isnan = function(x) { return typeof x === 'number' && isnan(x); };

**
note
**
: the coercion that is being blocked here is related to
```
isnan
```

. as an exercise, compare the result of
```
isnan(2)
```

,
```
isnan('2')
```

and
```
isnan('two')
```

.

fortunately, for the upcoming ecmascript 6, there is
```
number.isnan()
```

which provides a true
```
nan
```

detection (btw, you can already use this function in the latest version of chrome and firefox). in the
latest draft
from april 2014 (rev 24), this is specified in section 20.1.2.4:

when the number.isnan is called with one argument number, the following steps are taken:

1. if type(number) is not number, return false.

2. if number is nan, return true.

3. otherwise, return false.

in other words, it returns
```
true
```

only if the argument is
*
really
*
nan:

console.log(number.isnan(nan)); // true console.log(number.isnan(math.sqrt(-2))); // true console.log(number.isnan('hello')); // false console.log(number.isnan(['x'])); // false console.log(number.isnan({})); // false

next time you need to deal with nan, be extremely careful!

Published at DZone with permission of Ariya Hidayat, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Comments