-2

I'm learning Javascript mechanics and I believe I just stumbled across a bug with isNaN().

Here is the test code.

var x = "1000";

Answer = isNaN(x);
console.log(Answer);

The console log returns "false" which indicates that Javascript looks at "1000" as a number. I thought that anything inside " " was considered a string. Evidently not always. If I'm wrong maybe somebody has some insight that can set me straight.

DR01D
  • 1,325
  • 15
  • 32
  • NaN has a very specific meaning with regards to floating point number representation. isNaN only tests for that. – Thilo Dec 11 '16 at 06:05
  • Dangit thanks! I searched for isNaN and that thread didn't come up. That thread has the answer. – DR01D Dec 11 '16 at 06:05
  • I haven't read the post, but I'm pretty sure javascript recognizes strings as numbers. – alexr101 Dec 11 '16 at 06:07

1 Answers1

5

Apparently, it's a feature, not a bug.

When the argument to the isNaN function is not of type Number, the value is first coerced to a Number. The resulting value is then tested to determine whether it is NaN.

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN

Robert Lozyniak
  • 322
  • 1
  • 7
  • 1
    Another noteworthy distinction: `Number.isNaN` does not do this coercion. `isNaN('A') === true`, while `Number.isNaN('A') === false`. – gyre Dec 11 '16 at 07:02