5

Although the name of the function isNaN refers semantically to the value isNaN, the native implementation returns true for some non-NaN objects like undefined or {}.

isNaN(undefined);
=> true

I think, e.g., underscore's implementation is much more intuitive/logical:

_.isNaN(undefined);
=> false

Why did the ECMA standard specify such a counterintuitive behavior?

Why didn't they design isNaN to return true only if the value to test is really isNaN and leave the burden to test for convertability-to-number on a separate isNumber function?

Doing it this way would have led to even more advantages like no double negative when testing for numbers:

if (isNumber(x)) { } // if x is a number

instead of

if (!isNaN(x)) { }   // if x is not not a number

Since they are going to introduce Number.isNaN() in ECMAScript 6 (which does exactly what you would expect from it), it looks like there are more people thinking like this.

So, why did they design isNaN the way it is in the first place? Was it just a wrong decision or was there a good reasoning to do so?

Max Truxa
  • 3,308
  • 25
  • 38
  • possible duplicate of [What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wat' talk for CodeMash 2012?](http://stackoverflow.com/questions/9032856/what-is-the-explanation-for-these-bizarre-javascript-behaviours-mentioned-in-the) – Diodeus - James MacFarlane Dec 16 '13 at 18:50
  • 1
    The ECMA standard was finalized at a point in time when thousands (if not millions) of pages relied on code working with the language as it stood at the time. – Pointy Dec 16 '13 at 18:51
  • 2
    _"I had to be done in ten days or something worse than JS would have happened."_ – Brendan Eich. – Alex Wayne Dec 16 '13 at 18:51
  • 3
    The referenced question is quite interesting, but how is this question a duplicate of it? – Max Truxa Dec 16 '13 at 18:58
  • I don't see how this qualified as an answerable question. What do you want to know, exactly? What "their" motivation was? "Their" frame of mind? Ask *them*, not StackOverflow..... – Chris Baker Dec 16 '13 at 19:16
  • 1
    Read the answer to the question http://stackoverflow.com/questions/21482500/mdn-object-is-alternative-proposal. Too bad your question got closed +1 ;) – user568109 Feb 05 '14 at 12:03
  • 1
    To answer your question read the page https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN – user568109 Feb 05 '14 at 12:08

0 Answers0