For example, I create constructor and initialize array of objects (JS)
function SomeConstruct(val1, val2) {
this.val1= val1;
this.val2= val2;
}
var someCons= new Array();
someCons[0] = new SomeConstruct(12, 40);
someCons[1] = new SomeConstruct(34, 42);
someCons[2] = new SomeConstruct(0,-5);
And then I create new object with index [5] though last index of array is [2]
someCons[5]=new SomeConstruct(43,232);
But this code works, but when I try to access objects [3] or [4] I get error
How can I prevent this behavior - for instance I have some loop where I add new objects to the array with some condition ( val1==1) but at some point this logic breaks and I miss some index of array as above and create [5] instead of [3] element ? Why does JS allows this kind of behavior ?