Skip to content

Array.prototype.map unexpected behaviour with parseInt #12292

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
ilya1st opened this issue Apr 9, 2017 · 4 comments
Closed

Array.prototype.map unexpected behaviour with parseInt #12292

ilya1st opened this issue Apr 9, 2017 · 4 comments
Labels
invalid Issues and PRs that are invalid.

Comments

@ilya1st
Copy link

ilya1st commented Apr 9, 2017

perhaps, I've found interpreter bug. I can reproduce them on Chromium 57.0.2987.98 and on node 6.10.0

I need filter number in array, so I did the following:

[ 134, 135, 1212, 33 ].map(parseInt);

So the result was following:

[ 134, NaN, 1, NaN ]

This is not correct behaviour of Array.prototype.map or parseInt or interpreter.
This code works corect:

[ 134, 135, 1212, 33 ].map((item)=>parseInt(item) );
@kenany
Copy link
Contributor

kenany commented Apr 9, 2017

This is because map is calling parseInt with two arguments: the element and the index. The index ends up being used as the radix. So, this is what's happening:

parseInt(134, 0) // => 134
parseInt(135, 1) // => NaN
parseInt(1212, 2) // => 1
parseInt(33, 3) // => NaN

edit: posted too early :p

@ilya1st ilya1st closed this as completed Apr 9, 2017
@ilya1st
Copy link
Author

ilya1st commented Apr 9, 2017

sorry this is not a bug.

@aqrln aqrln added the invalid Issues and PRs that are invalid. label Apr 9, 2017
@aqrln
Copy link
Contributor

aqrln commented Apr 9, 2017

@ilya1st yeah, it's not a bug, just a common mistake many people (especially beginners) do :)
But if you ever actually find an bug in V8, feel free to submit it directly to the V8 issue tracker. Thanks!

@ilya1st
Copy link
Author

ilya1st commented Apr 9, 2017

@aqrln yes this is just about too much coding this week :)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
invalid Issues and PRs that are invalid.
Projects
None yet
Development

No branches or pull requests

3 participants