I have an array of objects with a length of at most 4 and at least 1. Here, I check which elements exist and do things accordingly.
function sendToGroup(receiver_group) {
if (receiver_group[0] !== undefined){
console.log(receiver_group[0])
}
if(receiver_group[1] !== undefined){
console.log(receiver_group[1])
}
if(receiver_group[2] !== undefined){
console.log(receiver_group[2])
}
if(receiver_group[3] !== undefined){
console.log(receiver_group[3])
}
}
When I give the array of 2 elements to this function, I see first and second element as expected in the console output but I also see an undefined in the line of
console.log(receiver_group[2])
How is this even possible? If it is undefined(which it is) this logging code should not get executed.
Edit: Chrome says the length of the array is 2. Which It is. receiver_group is an array. which has the content of
[{id:12, name:"name", age:"21"}, {id:22, name:"name", age:25}]
Also same thing doesn't happen for the item 4 which has the index of 3.