⚙️ JS Tips #1: Can you repeat?

Did you know that, in JavaScript, you have multiple ways of creating loops?

Mina Pêcheux


The loop is a neat thing in programming that is at the core of a great deal of algorithms — because, after all, many of those processes are about repeating the same instructions on evolving data or multiple instances.

So be it because you want to sort a table, find a path in a graph, generate a sequence of numbers, convert database rows to another format… you’ll probably need a loop at one point in your dev life!

And did you know that, in JavaScript, there is actually several ways of creating a loop? So, today, we’re going to explore 4 common techniques for looping about in vanilla JS (no package required!)…

The basic for-loop

Like many other programming languages, JavaScript has a for instruction that allows you to iterate until a condition is reached. For example, this code:

for (let i = 0; i < 5; i++) {
/* output:

would log all the numbers between 0 and 5 (excluded) in the console.

As explained on the MDN docs, the anatomy of this instruction is therefore as follows:

for (initialisation; condition; afterthought) {…



Mina Pêcheux

I’m a freelance full-stack web & game developer. I’m passionate about topics like CGI, music, data science and more! Find me at: https://minapecheux.com :)