isNaN vs Number.isNaN
Michael Otu

Michael Otu @otumianempire

About: Be allergic to a boring life, however be bored and enjoy it

Location:
Accra, Ghana
Joined:
Jul 28, 2020

isNaN vs Number.isNaN

Publish Date: Sep 3 '24
1 1

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
Enter fullscreen mode Exit fullscreen mode

Check out this article from MDN

Comments 1 total

  • leob
    leobSep 3, 2024

    Nice to know - incredibly confusing how they designed/named these functions ...

Add comment