Member-only story
Functional JS from Α to Ω: forEach
[Α] Using forEach as a diving board to understand Functional Programming
We control complexity by building abstractions that hide details when appropriate.
– SICP, 1979

We JavaScript coders have the chance to practice a multi-paradigm language supporting functional programming.
Do you want to learn that power?
Let’s start with 24 lessons, from Α to Ω, solving our everyday problems. Lessons overview: α. forEach, β. map, γ.filter.
A Simple List Iteration
Consider the following data: a list of strings.
JavaScript is not strongly typed, so a collection bigger than langs could be buggy. Let’s pretend we care, iterate on it, and check the console to see what we get there.
We learned how to do that since day one of our programming journey.
For us, it looks like very intelligible code. But is it really?
It’s an imperative approach, since it describes how the computer should iterate, not how humans think about iteration.
It’s not as imperative as an if
combined with a goto
, but still.
And this code contains several other inherent defects:
- It’s not reusable / not DRY (we have to rewrite all of this for every similar case).
- Not decomposable, re-composable, since it’s a block of procedural code
- Not easily maintainable: typos can hide here, and what if something changes the value of i during the loop? It could lead to bugs.