Codementor Events

Is NAN Not a Number?

Published Feb 06, 2020

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
Discover and read more posts from Janaka Dissanayake
get started
post comments1Reply
Janaka Dissanayake
5 years ago

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

https://frontendmasters.com/courses/deep-javascript-v3/nan-isnan/