Skip navigation

Tag Archives: divided by zero

In Javascript subtracting a string 5 from numeric 5 is legal and it will give you the correct result !!.

But when subtracting 5 from a string “five” it will not provide you the correct answer but javascript will not throw any error either!. In fact, it will return a value !!. The value is NaN (Not a number) and its type is number!!!.

alert(5-'5');      \\ 0
alert(5-'five');   \\ NaN (no errors)
alert(typeof NaN); \\ number

If a number is divided by zero, javascript returns a numeric value!!! The value is infinite. Yes it is of type number!!!

alert(5/0);   \\ infinity
alert(typeof infinity); \\ number

So, in javascript not only 1, 2, 3, etc., are numbers but also NaN and infinite!!