JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Using For Loops with Arrays

Adam Maley
Adam Maley
5,854 Points

Why is Console.log(students[i]); Showing the name of a person in the array and not the number that var i= ?

Why is Console.log(students[i]); Showing the name of a person in the array and not the number that var i= ?

Because students[i] is saying get the array value of students[whatever number i is], and returns an array value. If you want to print out the value of i, just do console.log(i);

1 Answer

Martin Lecke
Martin Lecke
14,353 Points

When you write students[i]. The i inside the bracket represents a number in your loop. If your write

students[0]; // represent first value in the array
students[1]; // second 
students[2]; // and third

The i variable increases for every loop iteration.