The For Loop Is Dead. Long Live the For Loop 👑

Kenton de Jong
4 min readMay 15, 2022

One of the most common things a JavaScript programmer does is loop through things. Got some elements? Loop em. Got some JSON? Loop em. Got some galleries? Loop them. Got some elements? Loop em. Got some JSON? Loop em. Got some galleries? Loop them. Got some elements? Loop em. Got some JSON? Loop em. Got some galleries? Loop them.

Sorry.

A common way for JavaScript developers to loop through things is with the for loop like so:

let parent = ['a', 'b', 'c', 'd', 'e'];for (let i = 0; i < parent.length; i++) {
console.log(parent[i]);
}
//a, b, c, d, e

Looks simple enough. So what happened to it? Why did it die? ☠️

Well, obviously, for loops still work. In fact, it’s used very often since it can be used for both arrays and objects without a problem.

The issue is that it isn’t very readable. For example, what is parent[i] ? Is it a (human) child? Is it an company? Is it some kind of hierarchy? It’s hard to know. It would be better if you could just read it and know instantly.

So, are the new loops actually needed? No. But for aesthetic reasons, the old loop must die 🗡️ And so, like when King Solomon died, the Kingdom of the For Loop has been split in half, severed forever, never to become…

--

--

Kenton de Jong

I am a web developer turned travel blogger that is forced to code to eat.