Arr[i] in a for loop

So the lesson name is:
“Basic JavaScript: Iterate Through an Array with a For Loop”
And then they have this example:

var arr = [10, 9, 8, 7, 6];
for (var i = 0; i < arr.length; i++) {
   console.log(arr[i]);
}
  1. I understand we have an array var arr = [10, 9, 8, 7, 6];
  2. Then we have a ( for loop ) for (var i = 0; i < arr.length; i++) { // the code }
    Now I understand how arrays work, and how the for loop works. But what I don’t understand is how arr[i] ads the value of i to the array. and how exactly it is adding the values of array if it has the value of i which is = 0 ?
    So when we do this arr[i] in the for loop, what is happening here?
    In the previous lessons they taught how to get an array’s value using bracket notation but they don’t explain it here.

arr[i] is not adding to the array, t is selecting the element at index i inside arr

then when you have console.log(arr[i]), that at each iteratiob is printing a different element of the array in the console

1 Like

I am stuck at this for 2 days now.
So when we do arr[i] are we getting the first item in the array?
Then because it’s inside a loop it will iterate though the array starting at 0 and the first time it will show the first item then the second time it will add numbers at position 0 with 1 right? and so on untill the condition is false
// Setup

var myArr = [ 2, 3, 4, 5, 6];

var total = 0;

for(var i = 0; i < myArr.length; i++) {

console.log(total += myArr[i]);

}

// Only change code below this line

Thank you, now it makes sense a little. But I’ll change the value of total to see what happens.
I still don’t know if this arr[i] has a name in Javascript.
It seems like some stuff have no name in Javascript so they skip the explanation part?

that’s bracket notation to access an array element, like previous challenge, the difference may be that i is a variable

1 Like