Javascript Error Values - NaN and Infinity

Javascript Error Values - NaN and Infinity

NaN and Infinity are two numeric error values of javascript which may be confusing for beginners so here is a brief explanation on NaN and Infinity.

Introduction

NaN and Infinity are two numeric error values of Javascript. The way these values function may look confusing so I created this blog explaining their weird functionalities.

NaN

NaN simply means "Not a Number" and is returned by a function when it has to return a falsy number value which indicating an error.

For example,

console.log(parseInt("hello world!")); // NaN

Other than functions, NaN can be generated in many ways.
Some of the operations which can generate NaN are Infinity / Infinity, 0*Infinity, 0 * -Infinity, 1 ^ Infinity, Infinity + (-Infinity).

You can view other operations which results NaN here.

NaN is a number?

Though NaN means "Not a Number", its type in Javascript is "number"...

console.log(typeof NaN); // number

It might sound strange, but NaN is a number value that is IEEE 754 ("Not a Number" value) as stated in Annotated ES5. In simple words, NaN is also a number but it cannot expressed as a valid number. Therefore, it is called as NaN.

Identifying NaN

Another weird functionality with NaN is...

console.log(NaN === NaN); // false

Now, why NaN === NaN is false? Well, it is the only javascript value which is not equal to itself. This happens because each NaN is a unique number which cannot be represented as a valid number. So, now how to identify NaN?

For that purpose, javascript has a function named isNaN...

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

The isNaN function will also return true for non numeric values too...

console.log(isNaN("string")); // true
// This is because Number("string") == NaN
console.log(isNaN({})); // true
// This is because Number({}) == NaN

To prevent this, you can use something like this...

const isNaNStrict = n => typeof n == "number" && isNaN(n);

Arithmetic operations with NaN

When using NaN for arithmetic operations, the result will be NaN too...

console.log(NaN + NaN); // NaN
console.log(NaN - NaN); // NaN
console.log(NaN * NaN); // NaN
console.log(NaN / NaN); // NaN

This is because "if either of the operand is NaN, the result will be NaN too" as stated in Annotated ES5.

Important Notes about NaN

  • NaN is a falsy value, therefore Boolean(NaN) == false.
  • When NaN is stringified into JSON, it is stringified into null JSON.stringify(NaN) == "null".

You can read more about NaN in Wikipedia.

Infinity

The Infinity value (represented as ∞ in mathematics) is a value returned by functions or operations when a number value is so big which the program or the computer cannot represent.

For example,

console.log(Math.pow(10, 1000)); // Infinity

Division by zero

Most of the programming languages will throw a error, when a number is divided by zero while in Javascript...

console.log(1 / 0); // Infinity

In mathematics, any number divided by zero is considered as undefined. For example, let consider 1 / 0 = x

1 / 0 = x
    1 = x * 0  ∵ Any number multiplied by zero is zero.
    1 = 0      ∴ Mathematically impossible.

So, why doesn't javascript basically returns undefined instead of Infinity. I don't consider this as a problem because in mathematics, Infinity's value is also undefined as Infinity is also mathematically impossible.

There is one exception here...

console.log(0 / 0); // NaN

It is not something very tricky, it is simply meaningless for 0 to be divided by 0 therefore the operation results a NaN value.

Negative Infinity

console.log(Infinity - Infinity); // NaN

Well, this was quite unexpected since ∞ – ∞ is undefined and javascript uses Infinity in such cases, then why the result is NaN?

This is because of the javascript compiler, a - b is considered as a + (-b), therefore Infinity - Infinity is considered as Infinity + (-Infinity) and javascript returns NaN on operations like Infinity + (-Infinity). In such cases, what can we do?

Javascript has a solution for that! And it is Number.NEGATIVE_INFINITY.

console.log(Infinity - Number.NEGATIVE_INFINITY); // Infinity
console.log(Number.NEGATIVE_INFINITY == (-Infinity)); // true

And now it looks like meaningful.

Arithmetic operations with Infinity

Here is how Infinity works with arithmetic operations.

console.log(Infinity + Infinity); // Infinity
console.log(Infinity - Infinity); // NaN
console.log(Infinity - Number.NEGATIVE_INFINITY); // Infinity
console.log(Infinity * Infinity); // Infinity
console.log(Infinity / Infinity); // NaN

Here is how Infinityand a numeric value works with arithmetic operations.

console.log(Infinity + 1); // Infinity
console.log(Infinity - 1); // Infinity
console.log(Infinity * 2); // Infinity
console.log(Infinity * 0); // NaN
console.log(1 / Infinity); // 0

Important Notes about Infinity

  • Infinity is a truthy value, therefore Boolean(Infinity) == true.
  • Though Infinity is a truthy value, when it stringified into JSON, it is stringified into null JSON.stringify(Infinity) == "null".
  • When Infinity is compared with any numeric value except NaN and Infinity, Infinity will be greater Infinity > 5 == true.
  • The parseFloat function can return Infinity as stated in the documentation while parseInt function will return NaN.
    console.log(parseFloat('Infinity')); // Infinity
    console.log(parseInt('Infinity')); // NaN
    
  • Unlike NaN, Infinity == Infinity will be true.

The End

If I missed to mention some functionalities of NaN or Infinity, let me know in the comments section.

If you find this blog informative,

Thank you for reading till the end!