In this question, I figured out that my problem was because I had accidentally incremented the Array.Prototype.length property using ++
What I'm wondering is why length isn't a read-only property of Arrays? I see that it isn't an oversight in the language's design, because MDN says specifically it can "SET or return" the length of an array. I'm just wondering why I'm able to screw up in that particular way? What kind of use case would merit being able to change the length of an array without changing the number of elements it contains?
I found this interesting article from JavaScriptTutotorial.net that explains why you might use this.
If you set length to zero, the array will be empty:
const fruits = ['Apple', 'Orange', 'Strawberry'];
fruits.length = 0;
console.log(fruits); // []
If you set the length property of an array to a value that is lower than the highest index, all the elements whose index is greater than or equal to the new length are removed.
The following example changes the length property of the fruits array to two, which removes the third element from the array:
const fruits = ['Apple', 'Orange', 'Strawberry'];
fruits.length = 2;
console.log(fruits); // [ 'Apple', 'Orange' ]
If you set the length property of an array to a value that is higher than the highest index, the array will be spare. For example:
const fruits = ['Apple', 'Orange', 'Strawberry'];
fruits.length = 5;
console.log(fruits); // [ 'Apple', 'Orange', 'Strawberry', <2 empty items> ]