DEV Community

Michael Otu
Michael Otu

Posted on

1

isNaN vs Number.isNaN

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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (1)

Collapse
 
leob profile image
leob

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more