3

In JavaScript on Chrome and Firefox:

isNaN( "\n" ) gives false
parseFloat ( "\n" ) gives NaN

The same is yielded for \t, \r and \f.'

  • \n is a number
  • Parsed \n gives you Not A Number.
  • Escaped characters such as the NULL byte \0, \\ and \" do work as expected.
  • We know that NaN is a number, just not representable by any other value
  • So \n is a number, that's not representable.

Why do browsers implement it this way?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Derk-Jan
  • 1,944
  • 16
  • 24
  • `parseFloat` might return `NaN`, but `+"\n"` returns `0`. There is a difference between *parsing* a string with `parse(Float|Int)` and simply converting it to a number. *"Why do browsers implement it this way?"* Because the specification dictates it. – Felix Kling May 31 '13 at 20:27
  • I figured that there is a difference. I suspected parsing requires actual numbers ("NaN being can't display that as a number"), but I couldn't find googling NaN and parsing. @FelixKling could you link me that specification? I love to understand exactly why. – Derk-Jan May 31 '13 at 20:31
  • `parseFloat`: http://es5.github.io/#x15.1.2.3, `isNaN`: http://es5.github.io/#x15.1.2.4 and `toNumber`: http://es5.github.io/#x9.3. – Felix Kling May 31 '13 at 20:36
  • See http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric/1830844#1830844 – Roatin Marth May 31 '13 at 20:40
  • `typeof "\n" // "string"` .. `parseFloat("apples") // NaN` Are `apples` a number? What makes you say "`\n` is a number"? – Paul S. May 31 '13 at 20:52
  • `isNaN( "apples" ) // true` `isNaN( "\n" ) //false` See the accepted answer why. – Derk-Jan Jun 01 '13 at 02:12

2 Answers2

7

Because the toNumber conversion of any string that is comprised only of white space (or if it's empty) results in 0.

console.log(Number("\n")); // 0

The parseInt/Float methods actually require some numeric content to be converted, though it'll allow leading spaces, and trailing garbage.

console.log(parseFloat("   123.45odsifjj")); // 123.45

The toNumber conversion is defined in 9.3.1 ToNumber Applied to the String Type.

A StringNumericLiteral that is empty or contains only white space is converted to +0

Derk-Jan
  • 1,944
  • 16
  • 24
  • 1
    The documentation has a nice example: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FisNaN of related strings that will produce false. – Steven Wexler May 31 '13 at 20:28
  • 1
    Thank you. This seems correct. I added the quote from the spec. – Derk-Jan May 31 '13 at 20:40
  • You're welcome. And thanks for the edit. Approved it as soon as I saw it. :-) –  May 31 '13 at 20:42
0

This is due to the dynamic typing when you call isNaN. "\n" is not a NaN (a value specified for floats and doubles), "\n" is a string.

EDIT: Apparently, when calling isNaN("\n"), "\n" is converted to a number first using ToNumber which does not have exact same behavior as parseFloat.

However, W3C w3schools says for parseFloat:

If the first character cannot be converted to a number, parseFloat() returns NaN.

This is what causes the asymmetry.

gzm0
  • 14,752
  • 1
  • 36
  • 64
  • `isNaN` converts the input to a number first though. See: http://es5.github.io/#x15.1.2.4. So the reason for `isNan("\n")` is not that the input is a string. And w3schools is not affiliated with W3C in any way. Please don't make the mistake and mix them up. – Felix Kling May 31 '13 at 20:32
  • 1
    @Derk-Jan: You should, they defined HTML, CSS, XML and lots of other standards. If you mean w3schools, I agree with you ;) – Felix Kling May 31 '13 at 20:33
  • @FelixKling OFF COURSE haha. That's what I meant. I just copied W3C from his answer. I saw the website, recognized it and closed it. – Derk-Jan May 31 '13 at 20:36
  • 1
    @Derk-Jan: I guessed so, that's why the ";)" :) – Felix Kling May 31 '13 at 20:37
  • 1
    Whoops... Seems I learned something here. Sorry for the mess. – gzm0 May 31 '13 at 20:49