Suppose you run this code:
var a = []; a[4] = true;
Then your array will look like [undefined, undefined, undefined, undefined, true]
But if you run this code:
var a = []; a.splice(4, 0, true);
You would get [true] instead, well, [undefined, undefined, undefined, undefined, true]
When using splicing, if the index exceeds the current length of the array, it simply stops at the last element.
Why is this the alleged behavior for splicing?
source share