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:
[image: 2019-11-06-151320_288x53_scrot.png]

- Leszek

On Wed, Nov 6, 2019 at 3:05 PM <[email protected]> wrote:

> 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.
>
> On Tuesday, October 15, 2019 at 12:20:18 PM UTC+2, [email protected]
> 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
> [email protected]
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/v8-dev/96f43c14-f89f-4523-9767-566f22d8828f%40googlegroups.com
> <https://groups.google.com/d/msgid/v8-dev/96f43c14-f89f-4523-9767-566f22d8828f%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
-- 
v8-dev mailing list
[email protected]
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-dev/CAGRskv-W0zmqaGtYLd3Ea7O4Wc0U--yJHBamdzY1erAMt6_YkQ%40mail.gmail.com.

Reply via email to