isNaN is not equal Number.isNaN (#tilPost)

I was on vacation last week and was reading Exploring ES6 by Axel Rauschmayer. And I can not say it enough – it is a great read and I constantly learn new tiny details about JavaScript. I highly recommend giving it a try!
ES6 is not…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

I was on vacation last week and was reading Exploring ES6 by Axel Rauschmayer. And I can not say it enough – it is a great read and I constantly learn new tiny details about JavaScript. I highly recommend giving it a try!

ES6 is nothing new today anymore and everybody talked/talks about the new shiny features but there is way more like the new method Number.isNaN and other unimportant looking additions.

So what's the deal with this new method? We already had the global function isNaN, or?

So, how do you usually figure out if a value is NaN? Well it turns out it's harder than you think because the global function is not a big help...

isNaN('foo'); // true
isNaN({});    // true
isNaN(NaN);   // true
isNaN(12);    // false

Using the global isNaN there are a lot of false positives and that's why I went with an equality check for years.

function myOwnIsNaN(value) {
  return value !== value;
}

That works because NaN is not equal to itself.

The new static method Number.isNaN fixes the odd behavior and actually works like you'd expect it.

Number.isNaN('foo'); // false
Number.isNaN(12);    // false
Number.isNaN({});    // false
Number.isNaN(NaN);   // true ?

Great, I like that!


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate
APA
Stefan Judis | Sciencx (2022-12-01T19:37:47+00:00) » isNaN is not equal Number.isNaN (#tilPost). Retrieved from https://www.scien.cx/2017/08/25/isnan-is-not-equal-number-isnan-tilpost/.
MLA
" » isNaN is not equal Number.isNaN (#tilPost)." Stefan Judis | Sciencx - Friday August 25, 2017, https://www.scien.cx/2017/08/25/isnan-is-not-equal-number-isnan-tilpost/
HARVARD
Stefan Judis | Sciencx Friday August 25, 2017 » isNaN is not equal Number.isNaN (#tilPost)., viewed 2022-12-01T19:37:47+00:00,<https://www.scien.cx/2017/08/25/isnan-is-not-equal-number-isnan-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » isNaN is not equal Number.isNaN (#tilPost). [Internet]. [Accessed 2022-12-01T19:37:47+00:00]. Available from: https://www.scien.cx/2017/08/25/isnan-is-not-equal-number-isnan-tilpost/
CHICAGO
" » isNaN is not equal Number.isNaN (#tilPost)." Stefan Judis | Sciencx - Accessed 2022-12-01T19:37:47+00:00. https://www.scien.cx/2017/08/25/isnan-is-not-equal-number-isnan-tilpost/
IEEE
" » isNaN is not equal Number.isNaN (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2017/08/25/isnan-is-not-equal-number-isnan-tilpost/. [Accessed: 2022-12-01T19:37:47+00:00]
rf:citation
» isNaN is not equal Number.isNaN (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2017/08/25/isnan-is-not-equal-number-isnan-tilpost/ | 2022-12-01T19:37:47+00:00
https://github.com/addpipe/simple-recorderjs-demo