Do you know what will be the output of this code?
JavaScript
console.log([1, 2, 3].map(parseInt));
Show answer and explanation
The answer is [1, NaN, NaN].
Array.prototype.map passes three arguments to its callback: (element, index, array). parseInt accepts two arguments: (string, radix). So the index becomes the radix on each call:
parseInt(1, 0) // radix 0 → treated as 10 → 1
parseInt(2, 1) // radix 1 → invalid (must be 2–36) → NaN
parseInt(3, 2) // radix 2 (binary) → 3 is not a valid binary digit → NaN
The fix, if you actually want [1, 2, 3], is to wrap parseInt so only the value is passed:
[1, 2, 3].map(n => parseInt(n))
Or use Number, which takes a single argument: ["1", "2", "3"].map(Number).