Your times variable start as 0. That's < 5, so you print "hey". Then times is 1; that's < 5, so you print "hey" again. You keep doing that and only stop when times == 5; then you've printed "hey" 5 times (remember we started at 0 😉).
When you use a for loop, the commands related to controlling the loop are bundled up after the for keyword, but they are all the same.
This will do the same thing. let i = 0 is executed first and only once; then, the code inside the brackets is executed if i < 5; at the end of each execution, i++ is called and the condition is checked again.
When dealing with arrays, you want to cycle through the indices in it:
letmyArray=['zeroth','first','second','third','fourth','fifth'];// The indices in the array go from 0 until 5. Let's get each one of those!letcurrentIndex=0;// Starting at the first index in the arraywhile(currentIndex<myArray.length){// Stopping when we reach the number 6console.log(myArray[currentIndex]);currentIndex++;}
Notice that an array [0] has an element at index 0 and a length of 1; an array [1, 2] has elements at indices 0 and 1 and a length of 2... The length is always 1 more than the last index, so you can use that to iterate on all the elements of any array.
When dealing with arrays, most of the times you start at 0, go all the way until before you reach the array length, adding 1 on each step. If you want to go from the end of an array to its beginning, it's the opposite:
You could also have written the condition in this last for loop as currentIndex >= 0. The important is executing the code when you are at index 0.
If you don't know the condition you need, stop and think about what you want to do. You need to do something a certain number of times? Then the condition will depend on the maximum number of times. You need to access the elements in an array? Then the condition will depend on the size of the array.
So, it all depends on what you want to do. I'd suggest you first start with
while
loops, and then you move on tofor
loops.Let's say you want to print "hey" 5 times:
Your
times
variable start as 0. That's < 5, so you print "hey". Thentimes
is 1; that's < 5, so you print "hey" again. You keep doing that and only stop whentimes == 5
; then you've printed "hey" 5 times (remember we started at 0 😉).When you use a
for
loop, the commands related to controlling the loop are bundled up after thefor
keyword, but they are all the same.This will do the same thing.
let i = 0
is executed first and only once; then, the code inside the brackets is executed ifi < 5
; at the end of each execution,i++
is called and the condition is checked again.When dealing with arrays, you want to cycle through the indices in it:
Notice that an array
[0]
has an element at index 0 and a length of 1; an array[1, 2]
has elements at indices 0 and 1 and a length of 2... The length is always 1 more than the last index, so you can use that to iterate on all the elements of any array.In
for
loop style:When dealing with arrays, most of the times you start at 0, go all the way until before you reach the array length, adding 1 on each step. If you want to go from the end of an array to its beginning, it's the opposite:
You could also have written the condition in this last
for
loop ascurrentIndex >= 0
. The important is executing the code when you are at index 0.If you don't know the condition you need, stop and think about what you want to do. You need to do something a certain number of times? Then the condition will depend on the maximum number of times. You need to access the elements in an array? Then the condition will depend on the size of the array.
I hope this helps 😄
This helps a lot. I'll practice some more. Thank you.