Sparse Arrays
Sparse arrays are arrays with empty slots (holes) where no element exists. They occur when you create an array with a length larger than the number of elements, or when you delete an element with delete. Sparse arrays are not common in modern JavaScript; it’s better to avoid them because they behave inconsistently with iteration methods.
Key Points
Section titled “Key Points”- Empty slots are different from
undefinedvalues. - Most iteration methods (like
forEach,map,filter) skip empty slots. - Methods like
findandincludestreat empty slots asundefinedin some cases. - Use
Array.from()to convert sparse arrays to dense (fill holes withundefined).
Code Example
Section titled “Code Example”// Creating sparse arraysconst sparse = [1, , 3]; // hole at index 1console.log(sparse.length); // 3console.log(sparse[1]); // undefined (but it's a hole)
const sparse2 = new Array(5); // empty array with length 5 (all holes)
// Deleting an element creates a holeconst arr = [1, 2, 3];delete arr[1];console.log(arr); // [1, empty, 3]
// Iteration skips holesarr.forEach((x) => console.log(x)); // 1, 3
// Converting to denseconst dense = Array.from(arr);console.log(dense); // [1, undefined, 3]