DEV Community

kycodee
kycodee

Posted on

A Quick Look Into Higher Order Functions

Out of all of the things I've learned on my journey as a software developer, higher order functions has been one of my favorites. Any function that takes in a function as a parameter or returns a function as an output can be considered a higher order function. We usually use these functions when dealing with arrays. Here's a little info on my three favorite higher order functions to use for a clean code environment.

the .Filter() method

The .filter() method is often used when we have an array of information and would like to return a new array, including only the elements that returned true to the conditions of the passed in function. The function that is passed into the filter method will have a parameter and the argument for that parameter will be an individual element in the array. Each element in the array will have a chance at this position. Once they are passed in as an argument, the function call will return either true or false. If the function call returns true, the element will be added to the new array. If the function call returns false, the element will not be added to the new array.

let array1 = [1, 2, 3, 4, 5, 6, 7, 8]

array1.filter(function(x){
    return x % 2 === 0
})
Enter fullscreen mode Exit fullscreen mode

In this instance, the filter function will return true if the value is an even number. If the number is even, it will be pushed into the new array and if the number is odd it will not.

the .Map() method

The .map() method is a bit different than filter, as it returns the result of calling a function on each element in the array. Whatever the result of calling the function on the current element is, will be added to a new array.

For Example:

let array1 = [1, 2, 3, 4, 5, 6, 7, 8]

array1.map(function(x){
    return x * 2
})

//output: [2, 4, 6, 8, 10, 12, 14, 16]
Enter fullscreen mode Exit fullscreen mode

In the above example, the function that is called is requesting the elements to be multiplied by 2. Once the elements are multiplied by 2, they will be added into the new array.

Last, but not least...the .forEach() method

The .forEach() method, unlike filter and map, doesn't return anything. forEach iterates over an array. It is often used to update elements in the array it is iterating over. For example:

let names = ['Jack', 'Peter', 'Rebecca'];

names.forEach(function(item, index){;
    names[index] = `Hi, my name is ${item}!`;
})

console.log(names);

//output:  [ 'Hi, my name is Jack!', 'Hi, my name is Peter!', 'Hi, my name is Rebecca!' ]
Enter fullscreen mode Exit fullscreen mode

The example shows an array of names, that is updated to be an array of the people in the array introducing themselves. This was done by updating each element in the array using the forEach method.

In conclusion, higher order functions can be very useful when trying to write clean code and accessing specific things in arrays. If you would like to level up your coding skills, I would definitely recommend taking a look at these functions!

Top comments (0)