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

Image of Wix Studio

2025: Your year to build apps that sell

Dive into hands-on resources and actionable strategies designed to help you build and sell apps on the Wix App Market.

Get started

Top comments (1)

Collapse
 
leob profile image
leob

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

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay