#33: ['1','2','3'].map(parseInt)
Topic: arrays · Difficulty: ★★★
console.log(['1','2','3'].map(parseInt));Output
[ 1, NaN, NaN ]
Why
Array.prototype.map calls its callback with THREE arguments: (element, index, array). parseInt also accepts a second argument — a radix — so map unintentionally passes the current index as parseInt's radix. For index 0: parseInt('1', 0) — radix 0 means 'auto-detect', so it parses as base 10, giving 1. For index 1: parseInt('2', 1) — radix 1 is invalid (radixes must be 0 or 2-36), so it returns NaN. For index 2: parseInt('3', 2) — base 2 (binary) has no digit '3', so it also returns NaN. This is one of the most famous 'wrong function signature passed to map' gotchas in JavaScript.