Firstly, don't use Array as an variable name - it is the javascript Array object, you don't want to override that
Secondly, since Array = {}, calling it any variation of the word Array is misleading, it's an Object
Thirdly, you've created Array.elements as an array, but then populate only indices 0, 3, and 4 - so you've created a sparse array to begin with
Fourthly, the code you wrote doesn't even run, since you are trying to assign .name to elements[0] but at that point elements[0] is undefined, so you'll end up with an error, and the code stops running right there
What it looks like you want is Array.elements = {} - then you can add/delete any key you want
For example
var obj = {};
obj.elements = {};
obj.elements[0] = {};
obj.elements[0].name = "Sally";
obj.elements[0].age = "20";
obj.elements[3] = {};
obj.elements[3].name = "Jack";
obj.elements[3].age = "21";
obj.elements[4] = {};
obj.elements[4].name = "Megan";
obj.elements[4].age = "22";
delete obj.elements[3];
for (let i in obj.elements) {
console.log('Number', i, 'is', obj.elements[i].name);
}
When using elements as an Array, you'll see after your initial population of the array, you already have two undefined to begin with - see first console output
Note: however, for...in loop will skip those undefined elements
Also note, the undefined elements are actually "empty slots" - i.e. if you simply put obj.elements[3] = undefined that's not the same as delete obj.elements[3]
var obj = {};
obj.elements = [];
obj.elements[0] = {};
obj.elements[0].name = "Sally";
obj.elements[0].age = "20";
obj.elements[3] = {};
obj.elements[3].name = "Jack";
obj.elements[3].age = "21";
obj.elements[4] = {};
obj.elements[4].name = "Megan";
obj.elements[4].age = "22";
console.log(obj.elements)
delete obj.elements[3];
console.log(obj.elements)
for (let i in obj.elements) {
console.log('Number', i, 'is', obj.elements[i].name);
}
Array, I wouldn't call that variable any variation ofArraysince it's not an Array; and finally just test fornullif you insist on.elementsbeing an Array (you could make it an Object instead, and you won't end up with a sparse array)