+ 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?

10 ответов

+ 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"));

+ 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

+ 7

NaN is a value of the type number that defines that the number is not a number. xD

+ 6

I get false
https://code.sololearn.com/W8EKIOUVXwN5/?ref=app

+ 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).

+ 6

Thanks for the answer.

+ 5

i am getting true
https://code.sololearn.com/WT0DFeVFdl9k/?ref=app

+ 5

" NaN is a number that is not a number."
It made me a bit more anxious about NaN.

+ 5

Just one thing if typeof is printing as number then isNaN should be False.

+ 4

@Tim This is creating confusion again please can you look at it
https://code.sololearn.com/WBdZi2iaBbXJ/?ref=app