[X]

map(parseInt)

luljs still works

Map parseInt over three identical strings and get three different answers.

In 2026: Still works. Array.map calls its callback with (value, index, array), and parseInt reads (string, radix). So mapping over three "10" strings runs parseInt("10", 0), parseInt("10", 1), parseInt("10", 2): radix 0 defaults to base 10 giving 10, radix 1 is invalid giving NaN, and "10" in base 2 is 2. Wrap it: x => parseInt(x, 10).

Where it came from: The most-cited map gotcha, warned about directly on MDN's map page: map hands the callback an index, and parseInt reads it as a base. MDN

<script>
var raw = ["10", "10", "10"];
document.write('<p><code>["10", "10", "10"].map(parseInt)</code> is <b>[' + raw.map(parseInt).join(', ') + ']</b></p>');
document.write('<p>Same string three times. map hands parseInt the index as a radix.</p>');
document.write('<p>Fixed: <code>.map(function (x) { return parseInt(x, 10); })</code> is <b>[' + raw.map(function(x){return parseInt(x,10);}).join(', ') + ']</b></p>');
</script>
sandboxed demo · breaks nothing but itselfrestart

More in luljs

all 63 in luljs › · the whole library ›