Surendar Vinayagamoorthy
Surendar Vinayagamoorthy

Surendar Vinayagamoorthy

NaN - It's Not A Number🙄

NaN - It's Not A Number🙄

Surendar Vinayagamoorthy's photo
Surendar Vinayagamoorthy
·Aug 22, 2020·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

According to MDN,

NaN is a property of the global object. In other words, it is a variable in global scope.

The initial value of NaN is Not-A-Number — the same as the value of Number.NaN. In modern browsers, NaN is a non-configurable, non-writable property. Even when this is not the case, avoid overriding it. It is rather rare to use NaN in a program.

Hear the value of NaN is Number.NaN. But, wait!

NaN === Number.NaN // false

Maybe because of the type you think🤔

NaN == Number.NaN // false

Somewhat reasonable explanation started here when I followed the links.

With a lot of confusion around NaN, let us see how we could arrive at a value of NaN. There are 5 different operations which could result in NaN.

  • Numbers that can't be parsed

    parseInt('Integer') ⇒ NaN
    Number('%^@#') ⇒ NaN
  • Math operation where the result is not a real number

    Math.sqrt(-1); ⇒ NaN
  • The operand of an argument is NaN

    NaN + 20NaN
    60 * NaNNaN
  • Indeterminate form

    0 * InfinityNaN
  • Any operation that involves a string and is not an addition operation

    "Integer" * 5NaN

For geekier discussions, reach out to me on twitter at @radnerus93, 📥 DM always open.

Share this