### 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

*, printed as*

**special infinity value****, when the result of a numeric operation is larger than the largest representable number**

*Infinity*Similarly, it * returns a negative infinity value*, printed as

*-*, when the resulting negative value becomes more negative than the most negative representable number.

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

**Look at the following code:-**

- 10 / 0; // => Infinity
- -10 / 0; // => -Infinity
- 2 / +0; // => +Infinity
- 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

*; instead it returns a special numeric value, called*

**does not throw an error***, and the program happily continues with execution. The following operations all result in*

**NaN (for Not a Number)***:*

**NaN****Look at the following code:**

- Infinity / Infinity; // => Nan.
- 0 / 0; // => Nan.
- "Hello" / 1; // => Nan. "Hello" cannot be converted to number
- Math.sqrt(-1); // => Nan.
- 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:**

- var profit = NaN;
- if (isNaN(profit)) {
- profit = 0;
- }

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:**

- var isNum = function isNumber(val) {
- if (typeof val === "number") {
- return isFinite(val);
- }
- return false;
- }
- var n = 3.6;
- alert(isNumber(n)); // => true
- n = NaN;
- alert(isNumber(n)); // => false
- n = Infinity;
- alert(isNumber(n)); // => false

**Happy reading!**

**Abhishek**