Interview Q&A / JavaScript

IntermediateQuick answer1 min readUpdated 3 Oct 2026

Why does ['1', '2', '3'].map(parseInt) not give [1, 2, 3]?

SHORT ANSWER

map passes three arguments to the callback: item, index and array. parseInt uses the second as the radix, so it runs parseInt('2', 1) and parseInt('3', 2), both NaN. Use map(Number) or map(s => parseInt(s, 10)).

Explain it simply

map calls your function with (item, index, array). Normally you ignore the extras. But parseInt(string, radix) takes two parameters, so the index sneaks in as the radix (the number base).

The calls are:

  • parseInt("1", 0): radix 0 means "use 10", so 1.
  • parseInt("2", 1): radix 1 is not valid, so NaN.
  • parseInt("3", 2): the digit 3 does not exist in binary, so NaN.

The fix is to control the arguments yourself.

Example

js
console.log(["1", "2", "3"].map(parseInt).join());           // Output: 1,NaN,NaN
console.log(["10", "10", "10"].map(parseInt).join());        // Output: 10,NaN,2

console.log(["1", "2", "3"].map(Number).join());             // Output: 1,2,3
console.log(["1", "2", "3"].map((s) => parseInt(s, 10)).join()); // Output: 1,2,3

Here parseInt("10", 2) reads 10 as binary, which is 2.

What to say in the interview

"map passes item, index and array. parseInt treats the index as its radix, so after the first element the results are NaN. I either pass an arrow function that fixes the radix at 10 or use Number as the callback."

Common mistakes

  • Passing any function with optional second parameters straight to map without checking its signature.

#javascript#arrays#puzzle

Related questions

Practise more JavaScript questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open JavaScript quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.