โ ๏ธ map() vs forEach()
forEach() always returns undefined! It cannot be chained. Use map() when you want a new transformed array, and forEach() strictly for external side effects.
โ ๏ธ filter() Truthiness Trap
filter() evaluates callback returns by truthiness, not strict boolean true. Values like 0, "", null, and NaN evaluate to falsey and are discarded!
โ ๏ธ ['1','2','3'].map(parseInt)
Produces [1, NaN, NaN]. parseInt(string, radix) takes a radix! map passes (item, index), so parseInt('2', 1) evaluates with invalid base 1, yielding NaN. Use n => parseInt(n, 10) or Number.
โ ๏ธ reduce() on Empty Array
Calling [].reduce(...) without an initial value throws a runtime TypeError: Reduce of empty array with no initial value. Always provide an explicit initial accumulator!
โ ๏ธ Default sort() is Lexicographic
[10, 9].sort() returns [10, 9] because items convert to strings and "10" precedes "9". Plus, sort() mutates in-place! Use (a, b) => a - b or toSorted().
โ ๏ธ Async Callbacks in map()
arr.map(async x => ...) returns an array of Promises, not the resolved data. You must wrap with await Promise.all(...) to wait for them.
โ ๏ธ Sparse Array Holes Skipped
Empty holes in sparse arrays (e.g. [1, , 3]) are skipped entirely by map, filter, forEach, etc. Callback is never executed for unassigned slots.
โ ๏ธ includes() vs indexOf() for NaN
[NaN].includes(NaN) returns true (using SameValueZero algorithm), whereas [NaN].indexOf(NaN) returns -1 because NaN !== NaN!