Q:

Odd Javascript behavior for array of strings

Someone pointed this weird array result in JavaScript and I was hoping someone could explain why this happens.
If I have an array like so:
var arr = ["10", "10", "10", "10"];

And I try to map those "10"'s to integers like so:
arr.map(parseInt);

The result will be an array with the values: [10, NaN, 2, 3] (I ran all of this code in Chrome's console)
I expected the result to be: [10, 10, 10, 10]
I understand that JavaScript can parse integers from string into different numbering systems, like binary, which I think explains where the 2 and 3 three are coming from, kind of. But why would parseInt change to parsing them in different bases? Could someone please explain why this is the result I get?

A:

Array.prototype.map accepts a callback which takes 3 arguments:

The current element
The index of the element
The array that this function was called on

parseInt takes up to two arguments:

The string to parse
(Optional) The radix i.e. the base of the number.

So in passing parseInt to map, you essentially get the following calls:
parseInt(arr[0], 0); // if 0, same as default
parseInt(arr[1], 1); // Parse as base 1
parseInt(arr[2], 2); // Parse as base 2 or binary
parseInt(arr[3], 3); // Parse as base 3

An easier and more effective way of accomplishing what you're trying is to use Number:

var arr = ["10", "10", "10", "10", "23", "74"];
console.log(arr.map(Number));

