Isn't this kind of negated by the fact that `delete arr[n]` working the way it does has required JavaScript arrays to never have been "real" arrays, in the entire history of the language?
Doesn't it? Try:
a = [ 6, 7, 8, 9, 10 ]
delete a[2]
for (x in a) { console.log(x) }If you click run there and look at the logged output, you’ll see:
1. The length remains the same after the delete statement
2. The items after index 2 keep their index
3. The for-of loop continues to iterate over index 2 (which now produces an undefined value on read)
It's an interesting model, and I think it definitely has its uses when reasoning about JS arrays. I'm not yet convinced it's the best model, but it's something I'm going to have to let percolate through my brain for a while to weigh it up properly. Thanks.
let a = [];
a[7] = 7;
for (let v of a) console.log(v);
> undefined, undefined, undefined, undefined, undefined, undefined, undefined, 7It actually removes the index.
> Object.keys([9,,9])
[ "0", "2" ]
The value of the `length` property does nothing to imply the presence or lack of any keys. This is covered in the "exotic objects" behavior section in chapter 10 of the ECMAscript spec.You can delete every property corresponding to an array’s indexes, and you’ll get the same result.