Functional JavaScript: forEach, map and reduce

Today, JavaScript is one of the most popular programming languages. Over time it has evolved and allowed to us build rich web applications and from a few years ago server applications also. However, writing code in JavaScript requires some attention, since the code could turn very messy easily. Functional Programming does not solve this problem at all, it is more an paradigm of writing code. It doesn’t tell you how do you structure your project as the popular frameworks like Angular or Vue.js.

This article is only about Functional Programming, I hope in the future to bring other articles about JavaScript and JavaScript Frameworks.

Now, let’s see some application of functional programming on JavaScript.

Considering the variable products that contains the properties name and cost:

var products = [
  name: 'Juice',
  cost: 4.2
  name: 'Rice',
  cost: 2
  name: 'Cookies',
  cost: 11.99

To print the description of all products on browser console we can build an function like this:

function logProducts(products){
 for (var i = 0; i < products.length; i++){
  console.log('product: ' + products[i].name);

This function makes an interaction through the list of products with for instruction to show the description of each product.

We can also specify the same behaviour with the follow function:

var logProducts = function(products){
 for (var i = 0; i < products.length; i++){
  console.log('product:' + products[i].name);

The difference now is that we haver an variable that contain an function (logProducts).

Let’s get more advantage from functions associated to variables:

var logProducts = function(products){
 for (var i = 0; i < products.length; i++){
  console.log('product: ' + products[i].name);
var doSomethingWithProducts = function(products, doSomething){
doSomethingWithProducts(products, logProducts);

The code was changed a little. Yet, Even in terms of behaviour nothing changes. The main difference is on pass of the function “logProducts” as a parameter to “doSomethingWithProducts”. It doesn’t look very useful at the moment, but is this functionality that allows to JavaScript apply the principals of functional programming.

To show the name of all products we had to create a for instruction and a variable to control the iteration (based on imperative programming).
However, if we use the forEach function we can simplify the code:

var logProductName = function(product){
 console.log('product: ' +;

Now we can see some advantages. The code is more clean and we do not need an auxiliary variable to iterate through the products.

And if we want to get the total cost of the products?

var sum = function(a, b) {
 return a + b.cost;
var total = products.reduce(sum, 0);

The reduce function returns only one value. The return is the accumulated result of the last call of the function “sum”.

Using other example, imagine that we need to get the element object for each identifier.
Let’s take a look on the follow code using imperative programming:

var ids = ['products','basket','categories'];
var listOfElements = [];
for (var i = 0; i < ids.length; i++){
 var el = document.getElementById(ids[i]);

Now the same behaviour using Map function:

var getElement = function(id){
 return document.getElementById(id);
var listOfElements =;

This “way” of programming requires some practice but in my opinion it allows to have more clean code and more adapted to the functionality that as consequence is more comprehensive.

This article was an little introduction to functional programming using the ForEach, Reduce and Map functions.

My 90's Video Games (Nintendo)

Video games is part of my life and of my childhood. In this article I’m gonna be a little “older” and talk about games that I enjoy most in the 90’s years. This is not about the better or worst games, it is about games that I used to play on my childhood and somehow they marked me in some way. At the moment I still enjoy play video games, but not sure why, it’s not the same as old times.

My first video game console was a Nintendo (The Classic NES). I received it in 1993 or 1994 and it was a really good surprise! Even when some neighborhood friends had already the Sega Megadrive (with “that rapid games” and stunning graphics…) I resist to that, I stayed with Mario and his friends.
I had five games and I played them a lot. Every time I turned off the console I had to start it all again because the option “save game” doesn’t existed yet. But as all kids, I had a lot of time and that was not a big issue.

Super Mario Bros was the first game I played. It came with the console. The classic sound, the colors, the scenarios. Even 5-6 years after the worldwide launch it stills very enjoyable. However, I don’t know why, but I can’t detail any specific part of this game that caused me some impact.

Super Mario Bros


Ninja Gaiden II. This game was a very hard one. Very good, with the main character climbing walls and ninja powers. But without checkpoints to progress the game it is almost impossible for me to progress. I did not choose the game, it just came to me. Fortunately, today I can enjoy it again on my Nintendo Classic Mini with the save option!

Ninja Gaiden II


My third game was Lion King. We are talking about 1994/95, the movie was a big hit on that time, and I just asked the game to my father. I was lucky! Honestly, the gameplay is not the best, but music (like the movie) and the scenarios with giraffes, hippos and monkeys…very nostalgic!

Lion King


The next game was my favorite of the five. Super Mario Bros 3, the best game ever! Mario more pretty and more powerful on 8 big worlds. Now, each world has a graphic map and I can make my own path to get the final boss… But again, the game was so big, passing all levels without turn off the console was impossible unless we use the “flute” power to jump to other worlds. The multiplayer was a must, on the battle mode (based on Mario Bros) Mario and Luigi compete for the first to defeat three or five enemies to win. It was a rush! Long hours “battling” with my cousin.

Super Mario Bros 3 – Level 1

Super Mario Bros 3 – World 1 Map


I implored for one soccer game to my father and luckily I was spoiled again! Tecmo Cup Soccer Game. I had to say, in the beginning I was a little disappointed but with time I liked it more and more. This is not a normal soccer game, now, I see it more as an “Cinematic Soccer” game with a mix of anime and role playing game. But at that time, I think I saw it as soccer game with Dragon Ball powers! It’s true, I can score a goal with a “hyper shot” or make a “wolf jump” to defense. It have a few types of shooting and defense that we’re going to unlock as we move through the game.
This game tells a complete story of Robin and his rivals and it is very engaging.
Searching on the web, I also discover that was the first game based on the manga Captain Tsubasa. Today I would love if any producer wants to make a remake of this game.

Tecmo Cup Soccer Game – Gameplay

Tecmo Cup Soccer Game

(This is my first article full in English. I want to start writing all of my articles in English, I’m not good on it but at least I’m practicing…)