+ 5

Difference between typeof and isNaN?

var x=1 alert(typeof(--x/x))//it gives number alert(isNaN(--x/x))//it gives true When type of that expression is number then why isNaN is giving true as the answer?

2nd May 2017, 5:28 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
10 Respuestas
+ 8
isNaN doesn't check the datatype. It checks the value. This for example also returns false because isNaN sees it as a number: alert(isNaN("2"));
2nd May 2017, 11:39 AM
Tim G
Tim G - avatar
+ 7
Since NaN(Not a Number) is not a datatype itself, it is of type number and just means that this number is not a number. It's a bit confusing but i hope you understand. NaN is a number that is not a number. :D
2nd May 2017, 11:28 AM
Tim G
Tim G - avatar
+ 7
NaN is a value of the type number that defines that the number is not a number. xD
2nd May 2017, 11:32 AM
Tim G
Tim G - avatar
2nd May 2017, 5:38 AM
Tim G
Tim G - avatar
+ 6
Ohhhh now i get it .. No it's not a bug. Your variable x is 1. In the first alert you decrease x by 1 and it results in "0/0". Zero divided by zero is not a Number. In the second alert x is again decreased by 1 and it results in -1/-1 what is a number(1).
2nd May 2017, 5:56 AM
Tim G
Tim G - avatar
+ 6
Thanks for the answer.
2nd May 2017, 5:58 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
2nd May 2017, 5:43 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
+ 5
" NaN is a number that is not a number." It made me a bit more anxious about NaN.
2nd May 2017, 11:30 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
+ 5
Just one thing if typeof is printing as number then isNaN should be False.
2nd May 2017, 11:35 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
+ 4
@Tim This is creating confusion again please can you look at it https://code.sololearn.com/WBdZi2iaBbXJ/?ref=app
2nd May 2017, 11:10 AM
Rishabh Agrawal
Rishabh Agrawal - avatar