User Name:

User Email:

This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2018 - First Crazy Developer (Abhishek Kumar)

crazydeveloper Javascript - Special numeric values: Infinity, NaN

When we talk about JavaScript, we always get different behaviour compare like other languges like in most languages, dividing a number by 0 throws an error and stops program execution. JavaScript however, returns a special infinity value, printed as Infinity, when the result of a numeric operation is larger than the largest representable number

Similarly, it returns a negative infinity value, printed as - Infinity, when the resulting negative value becomes more negative than the most negative representable number.

In both cases, the program simply continues with its execution!

Look at the following code:-

  1. 10 / 0; // => Infinity
  2. -10 / 0; // => -Infinity
  3. 2 / +0; // => +Infinity
  4. 2 / -0; // => -Infinity

We can see  the -0 in the last statement. Negative 0 means it is a negative value closer to 0, but the value is so small that it cannot be represented with available precision.

For instance, when you take -Number.MIN_VALUE (the smallest possible negative number, i.e. -5e-324) divided by 5, you get -0. Similarly, -1 divided by Infinity returns -0. Note that according to IEEE 754 specification, -0 is equal to 0

In JavaScript when an arithmetic operation fails it does not throw an error; instead it returns a special numeric value, called NaN (for Not a Number), and the program happily continues with execution. The following operations all result in NaN:

Look at the following code:

  1. Infinity / Infinity; // => Nan.
  2. 0 / 0; // => Nan.
  3. "Hello" / 1; // => Nan. "Hello" cannot be converted to number
  4. Math.sqrt(-1); // => Nan.
  5. NaN / 0; // => Nan. Any operation with NaN results in NaN

We can see here the JavaScript typeof operator does not distinguish between NaN and numbers, therefore the expression typeof NaN === "number" returns true. Furthermore, NaN does not compare equal to any number, including a comparison with itself, thus NaN == NaN returns false.

We need to be aware how NaN can affect computations which involves multiple steps. If we have a sequence of arithmetic operations and we get NaN as the final result, it means that either one of the initial inputs was NaN or NaN is produced somewhere in the chain.

The built-in function isNaN is used to determine whether a given value is NaN. It will return true if it is, and false if not. When a value is confirmed to be NaN, it is up to we , how we deal with the value. We could do something like this (set it to zero).

Look at the following code:

  1. var profit = NaN;
  2. if (isNaN(profit)) {
  3. profit = 0;
  4. }

If we go through the above code rather than using isNaN, a better approach would be to use the built-in function isFinite to determine whether a value can be used as a number or not. This function checks for both Infinity and NaN. Before calling isFinite make sure that the value passed is actually a number, like so:

Look at the following code:

  1. var isNum = function isNumber(val) {
  2. if (typeof val === "number") {
  3. return isFinite(val);
  4. }
  5. return false;
  6. }
  7. var n = 3.6;
  8. alert(isNumber(n)); // => true
  9. n = NaN;
  10. alert(isNumber(n)); // => false
  11. n = Infinity;
  12. alert(isNumber(n)); // => false

Happy reading!

crazydeveloper Home Page 10 August 2015

Become a Fan