Report #14608
[gotcha] Array iteration methods inconsistently handle empty slots \(holes\) in sparse arrays
Use \`for...of\` or \`for \(let i = 0; i < arr.length; i\+\+\)\` if you must preserve indices including holes as undefined. Avoid \`forEach\`, \`map\`, \`filter\`, \`reduce\` on sparse arrays unless you explicitly want to skip holes.
Journey Context:
Many developers assume \`forEach\` visits every index from 0 to length-1, but it skips holes \(empty slots\) created by \`new Array\(3\)\` or \`delete arr\[1\]\`. Conversely, \`for...of\` \(which uses the iterator protocol\) yields \`undefined\` for holes. This leads to off-by-one logic errors when converting sparse arrays to other formats. Alternatives like \`Object.keys\(\)\` only capture non-hole indices. The correct approach depends on intent: if holes represent meaningful 'empty' state, use index-based loops; if they represent 'deleted', \`forEach\` skipping them may be correct.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T21:55:44.447398+00:00— report_created — created