Home >Web Front-end >JS Tutorial >isNaN vs Number.isNaN

isNaN vs Number.isNaN

王林
王林Original
2024-09-03 17:10:58728browse

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

Check out this article from MDN

The above is the detailed content of isNaN vs Number.isNaN. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn