When I was a begginer coder, I once saw a version of fizz buzz that was bit of smoke at the time, looking back, I know get the value from what I saw. High order functions is a concept fairly use in the Javascript world, they are basically functions that can take another function as an argument, they are mainly apply to reuse and extend logic across our apps, some people say we sometimes use them without knowing, which is true, but we might only get to understand then well once we write our own, not only when using build methods like map, reduce or filter.

Let’s break down the fizz buzz example mention previously.

  const makeDivision = number => {
    return divider => {
      return number % divider == 0;
    }
  };

This function takes an argument called number to then return an anonimous function that takes his own argument called divider returning our final logic.

Now we can do:

    makeDivision(9)(3); // true
    makeDivision(9)(5); // false
  

I know the double bracket is a bit odd ( ()() ), is our way to access both parameters ( number and divider ).

A more readable and extendable way can be:

    const isDivideByThree = makeDivision(3);
    const isDividedByFive = makeDivision(5);
    const isDividedByFifthteen = makeDivision(15);

    isDivideByThree(3) // true
    isDivideBythree(5) // true
  

We can finish it by doing:

    const play = number => {
      if(isDivideByThree(number)) {
        return 'Fizz'
      }
      if(isDivideBythree(number)) {
        return 'Buzz'
      }
      if(isDivideByFifthteen(number)) {
        return 'Fizz Buzz'
      }
        return number;
    };

    play(3) // Fizz
  

A finder example

Let's do a finder function, that will make us find whatever object inside an array depending on the key and value we pass.

Our main function will look like this:

    const finder = (arr, key, value) => {
      return (value) => {
        return arr.filter(e => e[key] === value);
      };
    };
  

Our array:

    const users = [{ name:'George', age: 100, job: 'tester' }, {name: 'Mike', age: 50, job: 'bar man'}];
  

The final result is:

  
    const findPersonByAge = finder(users, 'age');
    const findPersonByJob = finder(users, 'job');

    findPersonByAge(100); // { name:'George', age: 100, job: 'tester' }
    findPersonByJob('bar man'); // {name: "Mike", age: 50, job: "bar man"}