[ACCEPTED]-Why check for !isNaN() after isFinite()?-google-closure-library

Accepted answer
Score: 21

The only difference is this:

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

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

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

Hope this helps.

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

Score: 2

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

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

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

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

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

Score: 1

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

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

Score: 1

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

More Related questions