Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why check for !isNaN() after isFinite()?

People also ask

What is the difference between isNaN () and isFinite () method?

isFinite function determines if the passed argument is finite value. It checks if its argument is not NaN , or negative infinity or positive positive infinity. isNaN on the other hand determines whether the argument passed is a NaN or not. This function is necessary because of the nature of NaN .

What is the purpose of isFinite function?

isFinite is a function property of the global object. You can use this function to determine whether a number is a finite number. The isFinite function examines the number in its argument. If the argument is NaN , positive infinity, or negative infinity, this method returns false ; otherwise, it returns true .

What is the use of isNaN () function in JavaScript?

isNaN() returns true if a number is Not-a-Number. In other words: isNaN() converts the value to a number before testing it.

What is NaN and how can we check for it?

In JavaScript, NaN is short for "Not-a-Number". In JavaScript, NaN is a number that is not a legal number. The Number. isNaN() method returns true if the value is NaN , and the type is a Number.


The only difference is this:

!isNan(1/0) // --> true
isFinite(1/0) // --> false

isNaN checks whether the argument is a number or not. The Infinities (+/-) are also numerical, thus they pass the isNaN check, but don't pass the isFinite check.

** Note that any string which can be parsed as a number ("2", "3.14") will cause isNaN to return false.

Hope this helps.

PS: The answer given by user1170379 was very nearly perfect.


you might reason out [Why?] after reading this:

NaN doesn't check if the passed value is infinite or not - it checks if the input val evaluates into a "Type: Number" end-result. Because isNaN(string) is accepted, so the: isNaN("3.14") //false (which means true, the given token is duck converted into a type Number successfully )

You may understand that the input value may happen to be an unresolved brute number, even a math operation as simple as: (x/y); which in turn might yield a (+/-infinity) number.

Here x=1, y=0; meaning (1/0).Then isNaN(x/y) will first evaluate to isNaN(1/0); then to isNaN(infinity) //false. Since (1/0)=infinity is of type: "number" ie typeof(1/0) //"number" isNaN should and will return false.

You don't want to put "infinity" where an end result number is expected.


Probably for the same reason that I have implemented (isfinite(num) && isfinite(-num)) - I was getting errors from mysql complaining about putting "-nan" into the database even though I had a check for isfinite(field)...

A useful article on this subject is http://jacksondunstan.com/articles/983 which provides an optimization ((d*0.0)==0.0)


isNaN() returns true if the argument is not a number or if the argument is a non-numeric value such as a string or an object.Otherwise, It returns false. Example: isNaN(0/0) =>true; isNaN(2-1) =>false; isFinite() returns true if the argument is a number other than NaN,Infinity or -Infinity.Otherwise, It returns false. Example: isFinite("2000") =>false; isFinite(200/2) =>true;`