Desk Clarity

A field guide to everyday digital problems.

Developer tools / Practical guide

Array Map with parseInt Produces NaN: Stop Passing the Index as the Radix

Map calls its callback with more than the element.

By ON Lab2 min readSources checked November 10, 2026 (KST)

Map calls its callback with more than the element. Passing parseInt directly lets the array index become its radix argument. Wrap the call and choose the radix explicitly.

Reproduce in an empty temporary directory

This isolated example creates only fixture data in its working directory or runs local JavaScript. Run it separately from your project so the named files cannot overwrite real work.

const assert = require('node:assert/strict');
const values = ['10', '10', '10'];
const accidental = values.map(parseInt);
const decimal = values.map(value => parseInt(value, 10));
console.log(accidental.map(v => Number.isNaN(v) ? 'NaN' : v).join(','));
console.log(decimal.join(','));
assert.equal(accidental[0], 10);
assert(Number.isNaN(accidental[1]));
assert.equal(accidental[2], 2);
assert.deepEqual(decimal, [10, 10, 10]);

Observed local output

10,NaN,2
10,10,10

What to compare

At index zero, radix zero triggers the normal inferred-radix behavior. At index one, radix one is invalid and produces NaN. At index two, the text 10 is interpreted in base two and becomes decimal two. A result can therefore be wrong even when it is a valid number. Checking only for NaN is insufficient to catch the full bug.

Choose the next action

Use a wrapper whenever a callback signature and a utility function signature do not match. For decimal integer prefixes, explicitly pass radix ten. If the complete string must be a valid number, define a stricter validation rule: parseInt accepts a valid prefix and can ignore following text. Decide how to handle empty strings, whitespace, signs, fractions and values outside safe integer precision before accepting external input.

Limits and verification

Using Number as a callback avoids the radix mismatch but changes conversion behavior; for example, an empty string converts to zero. It is not an automatic equivalent replacement. The fixture uses decimal digit strings and validates both the unexpected and corrected arrays. It does not claim that either conversion policy is appropriate for every identifier or user-entered field.

Related: JavaScript Sort Puts 10 Before 2: Use a Numeric Comparator and Choose Mutation.

Isolated example executed on macOS on November 10, 2026 (KST), using Bash 3.2, Python 3 for the byte fixture, and Node v26.5.0. Results describe the example, not your application or every runtime version.

Sources & further reading