Skip to content

Issue with "A re-introduction to JavaScript (JS tutorial)": Should use Number.isNaN rather than regular isNaN #3026

@nathnolt

Description

@nathnolt

MDN URL: https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript

What information was incorrect, unhelpful, or incomplete?

In the article (Under Number) it says that "You can test for NaN using the built-in isNaN() function:"
However, this fuction also returns true on lots of other inputs, other than NaN

isNaN('hello') // true
isNaN(function(){}) // true
isNaN({hello: 'world'}) // true

But other than that, it is inconsistent as well:

isNaN([1]) // false
isNaN([1,2]) // true
isNaN('1') // false

Specific section or headline?

under Numbers / You can test for NaN using the built-in isNaN() function:

What did you expect to see?

Instead of isNaN, Number.isNaN produces the expected result:

Number.isNaN('hello') // false
Number.isNaN(function(){}) // false
Number.isNaN({hello: 'world'}) // false
Number.isNaN([1,2]) // false

Browser support is a little bit worse for Number.isNaN when compared to isNaN but it's supported in all the current browsers, plus that if you actually need it, you can always use a polyfill, as seen on
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN#polyfill

Did you test this? If so, how?

Using Chrome DevTools

MDN Content page report details

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions