DEV Community

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

Collapse
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 Array.prototype.map.

var new_array = arr.map(function 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!

Collapse
lqj profile image
QJ Li Author

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

developer.mozilla.org/en-US/docs/W...

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

Yep! I'm aware. Pretty cool.