27 Loop the Loop



So I'm getting the TypeError, 'family[i] is undefined'. However, this code looks exactly like those that have been working here. Any ideas? Thanks!

function Person(name,age){
var family=new Array();
// Now we can make an array of people
family[0]=new Person("alice",40);
family[1]=new Person("bob",42);
family[2]=new Person("michelle",8);
family[3]=new Person("timmy",6);
// loop through our new array
for(var i=0; i <= family.length; i++){


Hi just remove tthe <= and put just <


Oh, Thank you!

Can you please explain why it would throw that error?


Hello @wizmarco, because when you make it <= family.lenght which is 4, the loop would try to iterate 5 times as we give i an initial value of 0. Our array however has 4 elements and when i = 4 and the loop tries to call family[4] it gives this error as the array does not have any element in that slot.