While exploring the use of .map
, I made an interesting discovery regarding arrays with holes, where certain indices are defined while others remain undefined:
// Holed
var array = [];
array[0] = 1;
array[2] = 3;
array // => [1, undefined, 3];
// Not Holed
var array = [1, undefined, 3];
array // => [1, undefined, 3]; The "same" as Holed
Interestingly, even though the two arrays above should be identical, they behave differently when iterated upon (as mentioned in the initial paragraph).
This raises a couple of questions:
- Is there a way to properly iterate over holed arrays?
- I have a suspicion that the exact bytes of these values differ and the unusual behavior is related to how JavaScript handles undefined values. Am I correct? Is there a deeper explanation for this inconsistency?
Any insights or assistance on this topic would be greatly appreciated. Thank you!