JavaScript Reference ยท Array
Array destructuring and sparse arrays
Concept guide to JavaScript sparse arrays: what holes are, how they differ from undefined, how methods treat them, and how destructuring reads arrays.
A JavaScript array is not a block of memory with fixed slots. It is an object whose index properties may be missing. An array with missing indexes is called a sparse array, and each missing index is a hole (also called an empty slot).
Holes are easy to create by accident, and they behave differently from slots that hold undefined. Destructuring, on the other hand, can skip positions on purpose with an elision such as [, second]. This page explains both ideas and how they interact.
How holes are created
Press Run (or Ctrl + Enter) to see the console output here.
No output.
A hole is not the same as an element whose value is undefined. 1 in [1, undefined, 3] is true, while 1 in [1, , 3] is false. Reading a hole with arr[i] still gives undefined, which is why the difference is hard to see.
How methods treat holes
Older iteration methods skip holes. Newer methods and the iteration protocol visit them and treat them as undefined.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
In short:
forEach(),map(),filter(),some(),every(),reduce()andindexOf()skip holes.map()keeps the hole in its result.for...of, spread,Array.from(),includes(),find(),findIndex(),entries(),values()and the copying methods such astoSorted()andwith()treat holes asundefined.Object.keys()andfor...inlist only indexes that exist.JSON.stringify()writesnullfor a hole.sort()moves holes to the very end, after theundefinedvalues.
Turning holes into real values
To get rid of holes, create a dense array:
const dense = Array.from(sparse); // holes become undefined const filled = new Array(3).fill(0); // [0, 0, 0] const squares = Array.from({ length: 4 }, (_, i) => i * i);
The last form is the usual way to build an array of a given size with computed values. new Array(3).map(...) does not work, because map() skips the holes.
Skipping elements in destructuring
Array destructuring reads values by iterating the array, so it reads holes as undefined. You can leave a position out of the pattern with an empty slot, which is also called an elision.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Notes on the pattern:
- Defaults apply when the value is
undefined, including a hole, but not fornull. - A rest element (
...tail) must come last and collects the remaining items into a real, dense array. - Destructuring
nullorundefinedthrows aTypeError. - Because destructuring uses the iterator, it works on strings,
Set,Mapand generators, not only arrays.
Practical advice
- Avoid creating holes. Prefer
[]andpush(),Array.from({ length: n }), ornew Array(n).fill(value). - Do not use
delete arr[i]to remove an element; it leaves a hole and keepslengthunchanged. Usesplice()ortoSpliced(). - Do not assign far beyond the end (
arr[1000] = x); some engines then store the array in a slower, dictionary-like form. - When a result looks like
[ <1 empty item>, 3 ]in the console, you are looking at a hole.
Browser support
Holes and destructuring are part of the core language. Destructuring arrived with ES2015 and works in all current browsers. Which methods skip holes is fixed by the specification, though newer methods consistently treat holes as undefined.