Let's skip all that... and get to the point. I like using Number.isNaN
but today, it seemed, I learnt why I choose it.
isNaN
and Number.isNaN
almost seems the same and they are both used to check if a value is NaN
. We usually do this when we cast or want to cast some value to a number. When do you use these?
Use isNaN
when you want to know if a value is numeric. Examples: "12", "2e4", etc are all numeric strings. If we want to check that such values are numeric, isNaN
is best.
Use Number.isNaN
when you specifically want to know if the value you are dealing with is NaN
.
isNaN
first converts the value to number and compares it to NaN
, Number(value) === NaN
.
This should summarize it:
> isNaN("hello")
true
> Number.isNaN("hello")
false
> Number.isNaN(parseInt("hello"))
true
Check out this article from MDN
Top comments (1)
Nice to know - incredibly confusing how they designed/named these functions ...