Why parseInt("1234fg",16) returns 74575 instead of NaN?

28 views
Skip to first unread message

kumom...@gmail.com

unread,
Oct 15, 2019, 6:20:18 AM10/15/19
to v8-dev
As the title says, is it a bug or is it implemented this way intentionally? If the latter, I don't understand why a valid number should be returned. 
Another mysterious behavior is parseInt("1234fg",37) and parseInt("1234fg",1) returns NaN as expected, but parseInt("1234fg",0) returns 1234...


Dan Elphick

unread,
Oct 15, 2019, 6:32:31 AM10/15/19
to v8-...@googlegroups.com
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

So in this case it's just ignoring the g and parsing everything before it.

On Tue, 15 Oct 2019 at 11:20, <kumom...@gmail.com> wrote:
As the title says, is it a bug or is it implemented this way intentionally? If the latter, I don't understand why a valid number should be returned. 
Another mysterious behavior is parseInt("1234fg",37) and parseInt("1234fg",1) returns NaN as expected, but parseInt("1234fg",0) returns 1234...


--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/231a509d-d01f-4e9f-9e38-27c82749b5bf%40googlegroups.com.

kumom...@gmail.com

unread,
Nov 6, 2019, 9:05:17 AM11/6/19
to v8-dev
Thanks for your response. I guess I didn't phrase my question not appropriately. I am actually curious why JavaScript does not return NaN for invalid inputs? Since the name of the method contains "parse", I assume it would throw some parsing error if the inputs are invalid.

Leszek Swirski

unread,
Nov 6, 2019, 9:17:08 AM11/6/19
to v8-dev
parseInt is one of the oldest parts of JavaScript, so it's likely that this is because it is (a generalisation of) the same behaviour as atoi in the C standard library, which I guess means you'd have to ask Ken Thompson. My guess would be because it made it possible to parse an integer out of a longer string without copying.

The reason you're getting NaN for parseInt("1234fg",37) and parseInt("1234fg",1) is not because of an invalid string, but an invalid radix. Specifically, only radices of 2-26 are allowed, and the function is specced to return NaN otherwise:
2019-11-06-151320_288x53_scrot.png

- Leszek

--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages