Sparse arrays in JavaScript
If there are gaps in the sequence of keys in the array, then you can get a sparse array. Let's see what it looks like. Let's have some array:
let arr = ['a', 'b', 'c'];
Let's add one more element to this array so that the sequence of keys have gaps:
arr[4] = '!';
As a result, a "hole", which has the value undefined, appears in the array:
console.log(arr); // shows ['a', 'b', 'c', undefined, '!']
The length of the array will consider
all holes. That is, in our case, it
will be 5
, and not 4
:
console.log(arr.length); // shows 5
Find out the length of the next array:
let arr = [];
arr[3] = 'a';
arr[8] = 'b';