5

This one says false, meaning the "" is a number:

alert(isNaN("")); 

This one says NaN, meaning the "" is not a number and cannot be converted:

alert(parseFloat(""));

I was expecting the second code to convert "" to 0 since "" is a number when tested in IsNaN but I was wrong! Am I getting crazy or I just missed something?

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
kazinix
  • 28,987
  • 33
  • 107
  • 157
  • 6
    `""` is a string. – Ismail Badawi Jun 07 '12 at 05:16
  • Hmm that is interesting. "" is just going to be an empty string though. Basically you just proved that isNaaN does not use the same code that parseFloat does – Matt Dodge Jun 07 '12 at 05:16
  • @isbadawi Thanks, I edited my question so you'll understand what I really want to achieve. – kazinix Jun 07 '12 at 05:18
  • 1
    http://stackoverflow.com/questions/825402/why-does-isnan-equal-false – Ayush Jun 07 '12 at 05:19
  • 2
    [MDN's isNaN page](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/isNaN) starts out with a warning, "Be careful, this function is broken." It goes on to explain why (and mentions the empty string explicitly). – nnnnnn Jun 07 '12 at 05:19
  • I wonder why Mozilla uses IEEE 794 and not IEEE 754... (does the former even exist?) – leppie Jun 07 '12 at 05:26

5 Answers5

4

parseFloat tries to parse a number from string where as isNaN converts the argument to number before checking it:

Number("") //0 http://ecma-international.org/ecma-262/5.1/#sec-9.3.1
parseFloat("") //NaN http://ecma-international.org/ecma-262/5.1/#sec-15.1.2.3

Apparently this is "broken" or "confusing", so from the specs:

A reliable way for ECMAScript code to test if a value X is a NaN is an expression of the form X !== X. The result will be true if and only if X is a NaN.

0 !== 0 // false
NaN !== NaN //true

function isExactlyNaN(x) {
    return x !== x;
}
Esailija
  • 138,174
  • 23
  • 272
  • 326
1

isNaN converts to the Number type and checks if the value is the special number NaN.

The empty string converted to a number is 0 (+"" === 0), and since 0 isn't NaN you see false.

parseFloat however is more complicated than a simple conversion to the number type, and it returns NaN for the empty string (parseFloat("") === NaN).

Paul
  • 139,544
  • 27
  • 275
  • 264
1

Being not NaN is not the same as being a number. It just means you aren't the very special value NaN.

Or anything convertible to NaN by the arcane conversion rules of the JS language.

Domenic
  • 110,262
  • 41
  • 219
  • 271
  • What does "NaN" mean again? Not-a-Number right? Removing the "Not-a" makes it a number :D at least in my perception. – kazinix Jun 07 '12 at 05:31
  • 1
    @dpp `NaN` is also a number: `typeof NaN === "number"` – Paul Jun 07 '12 at 05:48
  • `NaN` is a special number value. Perhaps it would be better to think of it as "the 'number' representing the result of operations like 0/0 or Infinity/Infinity" – Domenic Jun 07 '12 at 06:21
0

Javascript interprets empty string as 0 which fails isNaN test. As for parseFloat tries to parse the number in the empty string which fails and returns NaN..

Ali Almahdi
  • 146
  • 4
  • Is it JavaScript that interprets it as a 0 or the isNaN function? Coz if it's JavaScript, then why parseFloat does not consider "" as 0? – kazinix Jun 07 '12 at 05:33
0

To understand behavior of this it's important to understand semantics.

isNaN checks whether value cannot be coerced to number, if calling it on value returns false, then it means that Number(value) will return valid number.

parseFloat parses number from a string, if string doesn't start with any number then it returns NaN, as no number to parse was found.

Programmers tend to overuse parseInt and parseFloat for coercion, when in most cases they should use Number, which is more strict and not that confusing.

Mariusz Nowak
  • 32,050
  • 5
  • 35
  • 37