AI basics & verificationPractical AI workflows

Why map(parseInt) turns the second item into NaN

When reviewing an AI-generated array transformation, compare function signatures rather than just names. map supplies an index, while parseInt interprets its second argument as a radix. A small adapter fixes that mismatch; it does not define every rule for the incoming data.

Close view of a computer screen displaying colored code
Illustrative photo: Vishnu Kalanad · Unsplash LicenseStock illustration for the topic, not a screenshot, test of the pictured equipment or product recommendation.

Key Takeaways

["10","10","10"].map(parseInt) yields 10, NaN and 2 because the second arguments are 0, 1 and 2. For these agreed decimal integer strings, use map(text => parseInt(text, 10)). That fixes argument adaptation, but parseInt("10px", 10) still returns 10.

In this article

Expand the actual calls

MDN’s map reference describes three callback arguments. Passing parseInt directly places the array index in its radix position; it is not equivalent to repeatedly calling parseInt(text) alone. The third argument does not repair the mismatch.

At index 0, radix 0 invokes the parser’s default interpretation. At index 1, the radix is invalid and the result is NaN. At index 2, binary 10 equals decimal 2. The second string need not contain any corrupt data for the result to fail.

Hold the text constant

Both drafts are authored examples, not captured AI replies. The three-item case was executed in Node.js 22.23.2 on December 1, 2026. Results were recorded with String per element: JSON serialization would otherwise turn NaN into null and obscure the diagnostic value. The input array remained unchanged.

Inspect in a modern JavaScript console or module

const cells = ["10", "10", "10"];
const wrong = cells.map(parseInt);
console.log(wrong.map(String)); // ["10", "NaN", "2"]

const decimal = cells.map(text => parseInt(text, 10));
console.log(decimal); // [10, 10, 10]
console.log(parseInt("10px", 10)); // 10
This parsing specimen was executed; no real spreadsheet cleanup or external model request was performed.

The text stays the same; the second argument changes
IndexTextEquivalent incorrect callResult
010parseInt("10", 0)10
110parseInt("10", 1)NaN
210parseInt("10", 2)2

An adapter is not a complete data policy

The parseInt reference describes integer-prefix parsing. Radix 10 fits the agreed decimal strings here, but does not reject the entire string 10px or preserve the fractional part of 10.5. Decimal quantities, strict whole-string validation and exact long identifiers require their own contracts.

Specify signs, fractions, whitespace, units and missing values before deciding to reject, preserve or convert them. The radix and identifier guide explains why leading zeros may belong to an identifier. Do not strip meaningful characters simply to obtain a numeric array.

Review both sides of a callback boundary

Place the supplying API’s argument order beside the called function’s signature and expected results. Then add nondecimal values, empty strings, 10px and fractions. This small adapter is not a universal number-cleaning function.

The numeric-sorting input contract addresses comparators and mutation. This article addresses callback adaptation; sorting, aggregation and delivery still need separate verification after parsing.

Further reading & sources