JavaScript for loops

My journey trying to find the one loop operator to rule them all

Newer JavaScript developers are bound to be confused by the many ways to do a simple loop. Everyone seems to use for (var i=0; i < array.length; i++), but is there a simpler syntax? How do I handle objects? And what is forEach?

This guide will teach you how to use each option while taking you through my journey to find the one for loop operator to use 100% of the time so I never have to think about it again.

1

You know it, you love it, the loop classic:

let fruits = ["apple", "banana", "jackfruit"];
for (let i = 0; i < fruits.length; i++) {
  console.log(fruits[i].slice());
}

It works but isn't there something nicer to use out there?

2

for...in is not what you are looking for. You might think you can do this:

// BAD CODE
let fruits = ["apple", "banana", "jackfruit"];
for (let fruit in fruits) {
    console.log(fruit.slice());
}

But this prints the following:

0
1
2

It prints the index of each item because the for...in loop iterates through an object's enumerable properties. For an object, that iterates through the object's keys. For an Array, that is actually the index.

We can fix the code by doing the following:

let fruits = ["apple", "banana", "jackfruit"];
for (let fruit in fruits) {
    console.log(fruits[fruit].slice());
}

But now we have a super confusing line there: fruits[fruit]

Add to that, Mozilla explicitly warns users not to use for...in if the order of the elements matters.

Oh JavaScript...

3

for...of is actually closer to what you'd expect from a simple loop operator. It works like this:

let fruits = ["apple", "banana", "jackfruit"];
for (let fruit of fruits) {
    console.log(fruit.slice());
}

This outputs what you would expect (hope at this point).

One major snag though, you do not have access to the key, which in this case is the index that for...in was printing. It's pretty common to need the index so this cannot be the solution I always reach for.

4

forEach is a method that can be called on Arrays. It looks like this in the wild:

var fruits = ["apple", "banana", "jackfruit"];
fruits.forEach(function(fruit) {
    console.log(fruit.slice());
});

Slice pun aside, this is pretty simple right? It isn't the most elegant syntax necessarily but it reads very well and it seems like it's going to do what it says it's going to do. Does this means it can be the tool you always reach for? For many yes, but there is one gotcha that breaks the deal for me. You cannot a break or continue operator within the loop and the return statement does nothing whatsoever.

I was so close to just accepting forEach but that snag was big enough that I could never feel truly secure reaching for it 100% of the time.

5

That's right. The OG for (var i=0; i < array.length; i++) syntax won in the end. Between the rapport I already had with this operator and the fact that every JavaScript developer I know is very familiar with it, I decided to stop seeking greener pastures and stick with what I knew.

6

Honestly though, if it's reasonable to include a third party library, lodash's loops will solve all of your problems. They look just like forEach but you can use break, continue, and they are significantly faster.

let fruits = ["apple", "banana", "jackfruit"];
_.forEach(fruits, function(element, i) {
  console.log(element);
});