The parseInt Gotcha
You'll learn why a seemingly innocent point-free expression can produce completely unexpected results.
The Problem
Consider this innocent-looking code:
['1', '2', '3'].map(parseInt)
You'd expect [1, 2, 3], right? Instead, you get [1, NaN, NaN]. What went wrong?
Understanding the Issue
The problem is arity mismatch. When you write .map(parseInt) in point-free style, you're passing parseInt directly as the callback. But map() passes three arguments to its callback:
- The current element
- The index
- The entire array
Meanwhile, parseInt() accepts two parameters:
- The string to parse
- The radix (number base: 2, 10, 16, etc.)
So what actually happens:
parseInt('1', 0, array) // 0 is falsy, defaults to radix 10 → 1
parseInt('2', 1, array) // radix 1 is invalid → NaN
parseInt('3', 2, array) // '3' isn't valid in binary → NaN
The index becomes the radix! The point-free style hid this incompatibility.
The Fix
Make the arity explicit with a unary wrapper:
['1', '2', '3'].map(str => parseInt(str, 10))
// [1, 2, 3] ✓
Or use Number instead (which ignores extra arguments):
['1', '2', '3'].map(Number)
// [1, 2, 3] ✓
Key Takeaway: Point-free style looks elegant, but can hide arity mismatches. Always verify that the function you're passing expects the same number of arguments the caller provides—especially with map(), forEach(), and reduce().