Sparse slots vs explicit undefined
Predict the callback count, the slot test and the comparison after spreading.
Code Snippet
const list = [, undefined, 5];
let calls = 0;
const mapped = list.map(x => { calls++; return x ?? 0; });
console.log(calls, 0 in mapped, 1 in mapped);
const spread = [...list];
console.log(0 in spread, spread[0] === spread[1]);Expected Output & Explanation
2 false true
true true map skips the absent slot and preserves the hole. Spreading an array consumes its iterator and writes undefined into the new array at index 0.
Module Context & References
JavaScript · Language output drills Trace closures, receivers, prototypes, coercion and array edge cases
Personal Notes · Markdown