Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

Why does [1, 2, 3].map(parseInt) return [1, NaN, NaN]?

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).

Take the full quiz Next question →

More Arrays questions

  1. Why does [33, 2, 8].sort() not sort numbers correctly?
  2. What happens when you assign past the end of a JavaScript array?
  3. What does Array.push() return in JavaScript?
  4. How do you remove duplicates from an array with Set?
  5. What does Array.from({ length: 3 }) create?

Related topics