Dealing with NaN and negative zero (-0)

How many times we came across so many popular libraries and application and have seen NaN, plenty! I know right 😅. Well there is no such way to avoid them because their occurence depends on lot of factor about how you wrote your code. But here let's find out how to test against them properly and some tips and tricks 🙃.

NaN

NaN is value representing Not-a-number. Its a property of the global object according to MDN. It can appear due to various types of operations like "Cake" / 2 and much more other ways. The intresting part of a NaN is that it ain't even equal to itself, So if you do

NaN == NaN // false
NaN === NaN // false

As you can see it returns false when checked against itself.

One of the old way to check if something is NaN was like :

function formInputName(name) {
  if (name != name) {
    // rest of code
  }
}

// Because NaN != NaN -> true

But with introduction of Number.isNaN() in ES5 checking for NaN became more (descriptive) and robust. It returns true if given value is NaN.

NOTE: there is also one other way of checking NaN i.e isNaN() but it has some caveats to it like isNaN(undefined) //true so I didn't discuss it here you can see MDN docs for it.

Negative Zero

Negative zero (-0) is something that we don't encounter more often in our day to day code, but its something that might come and checking against it is a little bit tricky.

var nikeStocks = -0

if (nikeStocks == 0) {
  alertNike()
}

Now above code might seems okay to you but there's problem, which is -0 === 0 returns true. So how do one test for -0 🤔. One immediate idea that strike my mind is Hmm How about if we do

if (nikeStocks < 0) {
  //rest of code
}

But -0 < 0 returns false (also intrestingly -0 > 0 returns same false).

So the best option to check for negative zero is by using Object.is()

Object.is(nikeStocks, -0) // true

Also Object.is(NaN, NaN) yields true so you can use this method to check for NaN aswell.