DEV Community

Janaka Dissanayake
Janaka Dissanayake

Posted on

Is NAN Not a Number?

Alt text of image

Recently Javascript became my main development language so I have to solve complex problems using javascript, one of the challenging tasks was in Numeric operation how to represent the absence of a numeric value, do we use

0           // 0 has a specific value 
undefined   // it is not numeric
null        // it is an object
false       // boolean type
-1          // another specific value

In IEEE spec give special bit pattern or values call NAN, it is called invalid number

typeof NaN  // "number"

So if we use numeric operation in a function we need to get a numeric answer

const ageCompare = (age,newAge) => { 
  return Number(age) - Number(newAge)

const result = ageCompare(10,2)     // 8
const result = ageCompare(10,"n/a") // NaN
const result = ageCompare("myAge","n/a") // NaN

Number.isNaN(result) ... // true

*** NAN is the only value that not equal to each other

NAN===NAN  // false

Please refer following a link that gives a good explanation by Kyle Simpson

Top comments (0)