I can't write a JavaScript for loop, and it does not matter
Tags: javascript, react, webdev
Published: 2020-10-02 08:44:00

---
canonical_url: https://daily.sebastienlorber.com/i-cant-write-a-javascript-for-loop-and-it-does-not-matter-ckfrzpby8004iv6s19cxq04vj
---

I've been using JavaScript daily for 7 years, and I'm not able to remember the syntax of a JavaScript for loop.

Despite this fact, I'm a rather successful freelance developer. Recently I even had the awesome opportunity to work for Facebook, as the [Docusaurus lead maintainer](https://github.com/facebook/docusaurus/issues/2336), writing the code for the framework that powers the documentation sites of Babel, Prettier, Jest, ReactNative...

I'll explain why I'm not able to remember such syntax, and why it does not matter much.

--- 

# My story

**TLDR**: I'm a functional programmer

I've really started programming at the beginning of my engineer degree, around 2004 (before that, I was only able to hack some scripts for Counter-Strike console or IRC).

Most of our school teaching was based on Java, but we also saw a bit of C, C++, OCaml. 

The first loop syntax I learned probably looked like this one:

```java
List<Integer> numbers = Lists.newArrayList(1, 2, 3);

for (int i = 0; i < numbers.length; i++) {
   System.out.println(numbers.get(i));
}
```

Before I came out of school, Java 6 brought some new, simpler syntax:

```java
List<Integer> numbers = Lists.newArrayList(1, 2, 3);

for (Integer number : numbers) {
   System.out.println(number);
}
```

At my first job, the [Google Guava](https://github.com/google/guava) lib brought some new verbose functional syntax to Java, and I was able to do weird things with it 😅.

```java
List<Integer> numbers = Lists.newArrayList(1, 2, 3);

Lists.newArrayList(Collections2.transform(numbers, new Function<Integer,Void>() {
  @Override
  public Void apply(Integer number) {
    System.out.println(number);
    return null;
  }
}));
```

This Guava lib got me intrigued by functional programming, and lead me to become a Scala developer since 2012, and I was finally able to use functional programming concepts (loops, but not only) without the ugly Java/Guava syntax.

```scala
val numbers = List(1, 2, 3)
numbers.foreach(println)
```

In 2013, [ReactJS came out](https://reactjs.org/blog/2013/06/05/why-react.html), and this totally changed my career path. At this time, I didn't like JavaScript much and was only able to hack some inline JQuery things in server-rendered pages. But as a startup CTO, I saw my team struggle with architecture, BackboneJS and RequireJS, and thought I had to become better at frontend to lead them.

AngularJS looked like the safer choice at this time, but a Scala developer colleague really pushed for React, which looked fancy and risky. All things made sense with the visionary post of David Nolen ([The Future of JavaScript MVC Frameworks](https://swannodette.github.io/2013/12/17/the-future-of-javascript-mvcs/)), and we finally adopted React in January 2014, as it seemed we would be able to use our functional programming knowledge to the frontend app as well, and make the UI more predictable.

Fast forward, it wasn't easy to be a React early-adopter for our critical app. All companies were building their own state management solution, trying to figure things out, [and so we did](https://github.com/stample/atom-react), based on the ideas of David Nolen to hold a single immutable state in an atom (I was able to get a [hacky time-travel working](https://www.youtube.com/watch?v=zxN8FYYBcrI) before Redux). 

Since then both the JavaScript language and the ReactJS ecosystem have progressed a lot, and it's very common to use functional programming principles nowadays.

# Why I can't write a JavaScript for loop?

As a long-time functional programmer, **I simply don't write for loops** very often. 

Like anything you don't use regularly, you end up forgetting the syntax.

Today, many of us use ES5+ syntax (or Lodash/Ramda...) and some functional constructs. Using `map`, `forEach`, `filter` are the most illustrated examples in the JS community.

```js
const numbers = [1, 2, 3]
numbers.forEach(number => console.log(number));
```

But we can go much further than that once we are more experienced with functional programming, and almost never write any for loops anymore. 

Don't get me wrong, it's not necessarily a goal to not write for loops anymore, and I'm not telling you that you should remove all for loops of your production codebase.

Very often there's an alternative syntax possible for your loops that might be more expressive and easier to understand. After a while, you end up seeing a for loop as an implementation detail of a more elegant functional abstraction.

This more expressive syntax is not only for loops, and you can as well see a functional abstraction being an implementation detail of another higher-level abstraction.

Let's consider we want to increment the age of 2 brothers.

```javascript
const brothers = {
  id1: {name: "Sébastien", age: 34},
  id2: {name: "Antoine", age: 23}
};
```

I very often see the `array.reduce()` operator used when a more expressive alternative was possible.

```javascript
function incrementBrothersAges() {
  return Object.entries(brothers)
    .reduce((acc,[id,brother]) => {
      acc[id] = {...brother, age: brother.age + 1};
      return acc;  
    },{})
}
```

You know what? **I really struggled to write this code**. 

My first attempt was not working at all (TypeScript would have helped).

```javascript
function incrementBrothersAges() {
  return Object.entries(brothers)
      // acc is the first arg 
      .reduce(([id,brother],  acc) => {
        acc[id] = {...brother, age: brother.age + 1};
        // we must return the acc here
      },{});
}
```

Yet, writing this kind of transform is idiomatic for me, using higher-level functional programming abstractions, such as `mapValues` (included in lodash).

```javascript
function incrementBrothersAges() {
  return mapValues(
    brothers, 
    brother => ({...brother, age: brother.age + 1})
  );
}
```

And I think nobody would argue that this is harder to read and maintain right? If junior developers are not familiar with functional programming, they'll catch up fast and get used to it. This might even be harder to learn `reduce`.

# Why it does not matter?

I don't write for loops (or `reduce`), but I know the concepts. I know that these loops exist in different syntaxes, that can be useful for different use cases, and how to make a choice with a good tradeoff (performance, readability...).

I'll illustrate this with a concrete example from my daily work that actually led me to write this article.

I had this async function that performs some long task for a given country.

```javascript
async function runCountryTask(country) {

  // Simulate a long async task (1 to 5 seconds)
  const taskDuration = 1000 + Math.random() * 4000;
  await new Promise(resolve => setTimeout(resolve, taskDuration));
  
  console.log(`Task completed for ${country}`);
}
```

This task had to be run for many countries, but the tasks should be run sequentially, not in parallel.

As I know the concepts, and I knew that the following would not work, as `Promise.all` would run all tasks in parallel.

```javascript
async function runAllCountryTasks() {
  const countries = ["FR", "EN", "US", "DE", "UK", "IT"];
  
  // runs in parallel
  await Promise.all(countries.map(runCountryTask))
}
```

I also knew that there were multiple possible solutions to solve this problem:
- use a third-party dependency exposing the higher-level async primitive I need
- use `Promise.then()` recursively
- use async/await, using a for loop syntax to iterate over a fixed-size array

I didn't want to introduce a new third party dependency just for a tiny utility function. 

I also knew that using `Promise.then()` recursively could be harder to read, write, and maintain. There are many ways to write such a recursion, one of them could be:

```javascript
async function forEachAsyncSequential(array, asyncFn) {
  await array.reduce((acc, item) => {
    return acc.then(() => asyncFn(item))
  }, Promise.resolve());
}
```

So I opted for a basic for loop, as it seemed the right tradeoff. 

As I'm totally unable to remember the syntax (`in` vs `of`, can I actually use `const`?), I had to actually google it, and it didn't take me long to be able to write the TypeScript code that will be shipped in production.


```typescript
export async function forEachAsyncSequencial<T>(
  array: T[],
  asyncFn: (t: T) => Promise<void>,
): Promise<void> {
  for (const item of array) {
    await asyncFn(item);
  }
}
```

```javascript
async function runAllCountryTasks() {
  const countries = ["FR", "EN", "US", "DE", "UK", "IT"];
  
  // runs in sequence
  await forEachAsyncSequencial(countries, runCountryTask);
}
```

Believe me or not, but I think It's the only for loop I actually wrote in JavaScript this year. And once it's written, I won't need to write it ever again (at least for this project), as it's now part of my functional programming abstractions, that I can reuse anywhere I need.

[JsFiddle playground](https://jsfiddle.net/y17c6et8/1/)


--- 

# Conclusion

It's not very important to remember every syntax details to be productive in your daily work, particularly when you don't use them often (on purpose), as you prefer to work with more expressive, higher-level abstractions. 

I had to google many things to write this article:
- Syntax for declaring a Java list
- Syntax for iterating a Java list
- Does `System.out.println` accept an Integer?
- Syntax for Scala string interpolation
- Is there a `forEach` in Guava (actually found [my own StackOverflow question](https://stackoverflow.com/questions/38251257/guava-iterators-for-nested-foreach))
- What are the possible syntaxes for iterating over a JavaScript array
- Signature of `array.reduce()`

Not remembering all this does not matter much, as long as I know what to look for.

In the same way, I don't know much about many other JavaScript things:
- prototypes: I think I never hard to use them directly in my entire life, and I'm fine
- classes: used them temporarily when I really had to in React
- JavaScript quirks: I know some of them, but simply avoid the others by using ESLint, `===`, TypeScript... it's not worth knowing all of them
- ...

The knowledge and concepts you learn are more easily transposable from one language to another. I was able to learn React and contribute to its ecosystem quickly, thanks to my functional programming background. 

I would argue that knowing how to do a recursive algorithm is more important than knowing the syntax of a for loop of a particular language. You will likely write many recursive algorithms in your career: the concept of recursion is not going anywhere anytime soon. But it's way more likely that you switch from one language to another from time to time. 

Hopefully, writing this post will help me remember the syntax for a while until I forget it again 🤪.

---

🙏 If you like this post, please like it, share it or comment it 🙏: 
- [Tweet](https://twitter.com/sebastienlorber/status/1311948662843551744)
- [Hashnode](https://daily.sebastienlorber.com/i-cant-write-a-javascript-for-loop-and-it-does-not-matter-ckfrzpby8004iv6s19cxq04vj)
- [Dev](https://dev.to/sebastienlorber/i-can-t-write-a-javascript-for-loop-and-it-does-not-matter-11jb)
- [Reddit](https://www.reddit.com/r/javascript/comments/j3r08h/i_cant_write_a_javascript_for_loop_and_it_does/)
- [HackerNews](https://news.ycombinator.com/item?id=24660465)

For more content like this, subscribe to [my mailing list](https://mailchi.mp/4ea4df0b54f7/sebastienlorber) and follow me on [Twitter](https://twitter.com/sebastienlorber).