JavaScript Oddities: NaN

JavaScript Oddities: NaN

by Kenny Porterfield | Sun, May 30 2021

Category: JavaScript

NaN in JavaScript

JavaScript is a language famously full of quirks. I thought it would be a fun exercise to start going through some of these quirks, and try to explain why JavaScript is behaving the way that it does in those instances. Without further ado, we'll get started with an oldie but a goodie that produces some unexpected results, NaN.

 

NaN doesn't equal NaN
console.log(NaN == NaN);
// false


It may be surprising to see that NaN will return false when compared to NaN. It's the only value in JavaScript that is not equal to itself. To logically figure out why this is the case, let's dive into NaN.

 

 NaN is a Number
console.log(typeof NaN);
// number

 

To begin diving into NaN, we'll first examine why NaN (Not-A-Number), is in fact, a number data type. This is another famously brought up example that seems counterintuitive to developers dealing with NaN. It's important to understand the purpose of NaN to understand why this is. 

 

NaN is intended to denote the result of a nonsensical operation. If you expect your method to return a value type of number then you have a problem if you get something else. That's why they made NaN of type number, so it can be returned by functions that are supposed to return numbers. The caller then just has to check if the value is NaN.

 

There are five different kinds of operations that return NaN:

  • Parsing numbers
const input = '10';
const num = parseInt(input);
console.log(num); 
// 10

const input = 'A10';
const num = parseInt(input);
console.log(num); 
// NaN
  • Using undefined as an operand
console.log(undefined * 5);
// NaN
  • Using NaN as an operand
console.log(NaN * 2);
// NaN
  • Using indeterminate forms
console.log(10 + 0 / 0);
// NaN
  • Passing invalid arguments to a Math function
// Math.sqrt() returns the square root 
// of a number, and only accepts non-negative 
// numbers

console.log(Math.sqrt(-1));
// NaN

 

Circling back to why NaN does not equal itself, it's an IEEE standard and there are some really good reasons for it.

 

It's crucial to remember that NaN is a number type and understand its whole purpose is to indicate that a nonsensical operation has taken place. By making NaN compare unequal to anything else, including itself, NaN is able to propagate through all calculations, infecting them like a virus. So if somewhere in a deep, complex program a calculation hits upon NaN, you don't accidentally stumble into a seemingly sensible answer. Otherwise by identity you might get (NaN / NaN) = 1, along with other consequences like (NaN / NaN) == 1,  (NaN * 1) == NaN, etc. If this were how NaN behaved, in complex calculations you could get wildly incorrect (or worse: subtly incorrect) results from your calculations with no obvious indicator as to why.

 

Think of NaN like an error in a mathematical context. You made an invalid calculation, so this is what you get: NaN.

 

In the event that you need to determine if a value is NaN, JavaScript has a global function isNaN() that returns true if its argument is NaN.

 

console.log(isNaN(NaN));
// true
console.log(isNaN(1));
// false
console.log(isNaN("1"));
// false
console.log(isNaN("One"));
// true

 

You can see that even here you have to be careful with certain applications of isNaN() due to JavaScript's type coercion. isNaN() will coerce "1" to 1 and return false, because well, 1 is a number. While any other string that couldn't be parsed into an integer will return true when passed into isNaN()



Check out other JavaScript blogs.

Comments

Login or create an account to leave a comment.

Recent Blog Posts

Get in touch

Kenny Porterfield
kjport3@gmail.com
LinkedIn

Or, send me a message from here: