DEV Community

Discussion on: In JS how do you know the right condition to use inside your for loop?

Collapse
 
pedrohasantiago profile image
Pedro S • Edited

So, it all depends on what you want to do. I'd suggest you first start with while loops, and then you move on to for loops.

Let's say you want to print "hey" 5 times:

let times = 0;
while (times < 5) {
  console.log('hey');
  times++;
}
Enter fullscreen mode Exit fullscreen mode

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.

for (let times = 0; times < 5; times++) {
  console.log('hey');
}
Enter fullscreen mode Exit fullscreen mode

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:

let myArray = ['zeroth', 'first', 'second', 'third', 'fourth', 'fifth'];
// The indices in the array go from 0 until 5. Let's get each one of those!
let currentIndex = 0; // Starting at the first index in the array
while (currentIndex < myArray.length) { // Stopping when we reach the number 6
  console.log(myArray[currentIndex]);
  currentIndex++;
}
Enter fullscreen mode Exit fullscreen mode

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:

let myArray = ['zeroth', 'first', 'second', 'third', 'fourth', 'fifth'];
for (let currentIndex = 0; currentIndex < myArray.length; currentIndex++) {
  console.log(myArray[currentIndex]);
}
Enter fullscreen mode Exit fullscreen mode

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:

let myArray = ['zeroth', 'first', 'second', 'third', 'fourth', 'fifth'];
for (let currentIndex = myArray.length - 1; currentIndex > -1; currentIndex--) {
  console.log(myArray[currentIndex]);
}
Enter fullscreen mode Exit fullscreen mode

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.

I hope this helps 😄

Collapse
 
scothinks profile image
scothinks

This helps a lot. I'll practice some more. Thank you.