DEV Community

Discussion on: Do you know these weird parts of JavaScript?

shreyasminocha profile image
Shreyas Minocha

I found that one interesting too. Took me some time.

I noticed that

['1', '7', '11'].map((n) => parseInt(n, 10))

works as expected. parseint is misinterpreting the second parameter somehow?

Then I saw docs for

var new_array = callback(currentValue[, index[, array]]) {
    // Return element for new_array
}[, thisArg])

If the callback accepts a second parameter, it passes the index of the element to it. parseint does accept two parameters, but of course, it doesn't expect an index in the third one.

    parseint('1', 0),
    parseint('7', 1),
    parseint('11', 2)

And sure enough, the output confirms my theory!

lqj profile image
QJ Li Author

you are close, if you take a look at the api doc, you will fully understand it.

parseInt(string, radix);
Thread Thread
shreyasminocha profile image
Shreyas Minocha

Yep! I'm aware. Pretty cool.