JavaScript Tutorial

Sparse Arrays

Normally, arrays have continuous memory. For example, the first element we store at location index zero (0), which is the first element in an array, followed by 1, 2, ... up to n number of index. We can store elements in an array like that.

In sparse array, we don’t need to store elements continuously. We can add an element at location index (0), then maybe at index (10). It's not necessary to use 1 ... n in chronological order.

That’s called a sparse array.

const arr = [];
arr[0] = 1;
arr[10] = 2;
console.log(arr.length); // 11 (sparse array)

Questions & Answers Related

No Q&A available for this topic.