1

I am a javascript beginner and I understand how to iterate over an array and how to use it, but I am trying really, really hard to understand WHY it works.

For example:

let myArray = ["one", "two", "three", "four"];

for(let i = 0; i < myArray.length; i++){ 

console.log(myArray[i]);
}

I understand what is going on in each of the 3 parts within the for loop, but I don't really understand how the iis accessing/communicating with/connected to/exchanging data with the array myArray. At which point in this code are we telling javascript that "i" is somehow connected to "myArray"?

At first I thought something was implied or implicit in the for loop itself, i.e., that when we write i < myArray.length it is somehow implying that i = myArray (that "i" is assigned to the value of whatever is in myArray). But upon further thought, i < myArray.length is simply the length of the array (in this case, 4), and doesn't really connect the two.

So this has opened up a whole conceptual can of worms for me about what the "i" really is here besides just a variable in a for loop. I have been thinking about the "i" as a sort of ghost/temporary variable that we create that will do the looping for us and then disappear once it is done (I am not even sure if that is the correct metaphor here).

I apologize in advance if I am not articulating this clearly, as I am just a beginner.

Thanks in advance.

ughdeeb
  • 21
  • 3
  • 1
    *"At which point in this code are we telling javascript that "i" is somehow connected to "myArray"?"* - `i < myArray.length` and `myArray[i]`. – jonrsharpe Jul 09 '20 at 18:38
  • 3
    Yes, `i` is just a regular variable that starts at `0` and gets incremented by `for` on each iteration, as long as it's less than the length of the array. In your example it's being used to access the different indices of the array in `myArray[i]`. For example, `myArray[0]` and `myArray[1]` ... etc. It's like if I had four numbered (indexed) bins and you wanted to see what was in each, you could come to me and say show me what's in the third bin, and that's the same as `myArray[2]` (since array indices begin at zero) – j08691 Jul 09 '20 at 18:40
  • Hi, thank you for your reply. I understand "it's being used to access the different indices of the array in myArray[i]" but I don't understand HOW is it being accessed? Is something implied somewhere? I don't see where we are explicitly establishing a relationship between ```i``` and ```myArray``` – ughdeeb Jul 09 '20 at 18:49
  • 3
    There is no relationship between `i` and `myArray`, you are just asking the `myArray` to give you the value at the index of the number in `i` at the time you do `myArray[i]` (e.g. `myArray[0]`, `myArray[1]`, `myArray[2]`, etc.). Arrays work like that because that is how they are defined to work. [Array on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) – crashmstr Jul 09 '20 at 18:59
  • I think I get it now, thanks for your help! – ughdeeb Jul 09 '20 at 19:27

1 Answers1

2

The variable i is simply just a variable. It is a number that is incremented with each iteration, and stops incrementing when it increments through all the indexes in the array. The variable i is not in connected with myArray except that it loops through all the indexes of myArray (0, 1, 2, 3). It starts with 0, and increments itself while doing the commands inside the for loop (getting a certain element in myArray using bracket notation).

E.g, the variable i starts at a value of zero. While being 0, myArray is called in the for loop, but specifically for the 0th index, which is "one". Then the variable i increments, and the code in the for loop calls the 1st index of the array, and so on until the conditional i < myArray.length is met, which marks the upper boundary of the indexes of the array.

I hope this helps!

Someone
  • 45
  • 7