Skip to content
JavaScript: Keep the array index out of parseInt

JavaScript: Keep the array index out of parseInt

November 19, 2026

Passing parseInt directly to map gives the array index a second job: it becomes the numeric radix. Identical text can then produce different results at different positions. A small wrapper supplies the radix explicitly.

Set up

Tested on Debian 13 Linux with Node.js v24.19.0. Use Bash, Node.js on your PATH, a text editor, and the mktemp, rm and rmdir utilities. No packages or network access are needed. Keep the same terminal through cleanup. Stop if setup fails before saving the source.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save as javascript-map-parseint-radix.mjs:

import assert from 'node:assert/strict';

const inputs = ['10', '10', '10'];
const accidental = inputs.map(parseInt);
assert.equal(accidental[0], 10);
assert.equal(Number.isNaN(accidental[1]), true);
assert.equal(accidental[2], 2);

function decimalPrefix(text) {
  if (typeof text !== 'string') throw new TypeError('Expected text');
  return parseInt(text, 10);
}

const decimal = inputs.map(decimalPrefix);
assert.deepEqual(decimal, [10, 10, 10]);
assert.deepEqual(['12px', '-7ms'].map(decimalPrefix), [12, -7]);
assert.equal(Number.isNaN(decimalPrefix('no digits')), true);
assert.throws(() => decimalPrefix(null), TypeError);
assert.deepEqual([].map(decimalPrefix), []);
assert.deepEqual(inputs, ['10', '10', '10']);

console.log('Direct callback:', accidental.map(String).join(', '));
console.log('Decimal wrapper:', decimal.join(', '));
console.log('Intentional prefixes:', ['12px', '-7ms'].map(decimalPrefix).join(', '));
console.log('Invalid text, type, empty input, and source checks: passed');

Run:

node javascript-map-parseint-radix.mjs

Observed output:

Direct callback: 10, NaN, 2
Decimal wrapper: 10, 10, 10
Intentional prefixes: 12, -7
Invalid text, type, empty input, and source checks: passed

Match the callback’s arguments

The direct callback produces 10, NaN and 2 from three copies of the same text. At index zero, radix zero lets the parser infer decimal for this input. Radix one is invalid, and radix two reads the text as binary.

The ECMAScript map algorithm supplies the value, index and source object to its callback. The parseInt definition uses its second argument as the radix. JavaScript allows the call, even though those argument meanings disagree.

The wrapper accepts the value and supplies decimal radix 10 itself. Extra callback arguments are ignored by the wrapper. Keeping the conversion in a named function also gives the type check a clear place to live.

Decide whether prefixes are acceptable

This helper intentionally reads integer prefixes. It accepts the numeric part of a measurement with a suffix, and returns NaN when no digits can be read. It rejects non-string input instead of letting the parser convert another type to text.

It does not validate a complete decimal field, check a unit suffix or guarantee a safe integer. For identifiers, counts or money, validate the complete format and the numeric range according to the field’s requirements.

The test uses a dense array of strings and preserves that input array. If your source may have missing array elements, validate that separately: mapping a sparse array does not call the callback for its holes.

Clean up

Remove the saved file and empty scratch directory. If the directory is not empty, inspect its contents before removing anything else.

rm -- javascript-map-parseint-radix.mjs
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Acknowledgements

This post was created with assistance from AI.

Last updated on