Sub topic 10
Common Pitfalls: map(parseInt), Side Effects, and Sparse Arrays
This chapter assumes you already know how map, filter, and reduce call their callbacks, and that each callback receives (element, index, array). The three bugs below come from that calling convention, from the difference between a new array and new objects, and from arrays with holes in them.
Pitfall 1: Passing a Function That Takes Extra Parameters
Passing a named function straight to map looks tidy. It breaks when that function has optional parameters, because map fills them with the index and the array.
console.log(['1', '2', '3'].map(parseInt)); // [1, NaN, NaN]
console.log(['1', '2', '3'].map(s => parseInt(s, 10))); // [1, 2, 3]
console.log(['1', '2', '3'].map(Number)); // [1, 2, 3]parseInt(string, radix) reads the index as its radix:
| Call made by map | Radix meaning | Result |
|---|---|---|
| `parseInt('1', 0)` | 0 means "guess", so base 10 | `1` |
| `parseInt('2', 1)` | base 1 is invalid | `NaN` |
| `parseInt('3', 2)` | `'3'` is not a binary digit | `NaN` |
The fix is to wrap the call in an arrow function so you decide which arguments get through. Number is safe here because it only uses its first argument, but it handles input differently: Number('12px') returns NaN, while parseInt('12px', 10) returns 12.
Pitfall 2: Side Effects Inside the Callback
map returns a new array, but the elements in it are the same objects unless you copy them. If the callback mutates those objects, the original data changes too.
const users = [{ name: 'Ada', active: false }];
// Bug: mutates the original object, and both arrays share it
const broken = users.map(u => { u.active = true; return u; });
console.log(users[0].active); // true (original changed)
console.log(broken[0] === users[0]); // true (same object)
const fresh = [{ name: 'Grace', active: false }];
const activated = fresh.map(u => ({ ...u, active: true }));
console.log(fresh[0].active); // false
console.log(activated[0].active); // trueSpread makes a shallow copy. Nested objects inside each element are still shared, so you need to copy them as well if you change them.
A related mistake is using map only for its side effects and throwing the result away. If you aren't using the returned array, use forEach or a for...of loop. That tells the reader the loop exists only for its effects.
const ids = [1, 2, 3];
ids.forEach(id => console.log('processing', id)); // not ids.map(...)Pitfall 3: Sparse Arrays
A sparse array has holes, which are indexes that were never assigned. A hole is different from a slot that holds undefined. The array methods skip holes and never call the callback for them:
const sparse = [1, , 3];
console.log(sparse.map(x => x * 2)); // [ 2, <1 empty item>, 6 ]
console.log(sparse.filter(() => true)); // [ 1, 3 ]
console.log(sparse.reduce((a, b) => a + b, 0)); // 4
console.log([1, undefined, 3].map(x => x * 2)); // [ 2, NaN, 6 ]map keeps the holes in its result, filter drops them, and reduce skips them. The <1 empty item> output is how Node.js prints a hole. Browsers display holes in their own way.
The usual way to hit this is building an array with Array(n):
console.log(Array(3).map((_, i) => i)); // [ <3 empty items> ]
console.log(Array.from({ length: 3 }, (_, i) => i)); // [0, 1, 2]
console.log(Array(3).fill(0).map((_, i) => i)); // [0, 1, 2]Array(3) sets the length but creates no elements, so map has nothing to visit. Use Array.from with a mapping function, or call fill first so every slot exists.
Exercise
What does ['10', '10', '10'].map(parseInt) return, and why?
Show answer
It returns [10, NaN, 2]. The calls are parseInt('10', 0), which uses base 10 and gives 10. Next is parseInt('10', 1), which gives NaN because base 1 is invalid. Last is parseInt('10', 2), which reads the string as binary and gives 2.
Exercise
A teammate writes const grid = Array(4).map(() => []); and then grid[0].push('x') throws. Explain the bug and fix it.
Show answer
Array(4) creates four holes, and map skips holes, so grid is still four holes. That makes grid[0] undefined. The fix is Array.from({ length: 4 }, () => []), which also gives each slot its own new array.