High Order Functions

Ahmed A.
3 min readJun 27, 2021

--

Last week we looked at the call-back functions, this week we will look at high-order functions. So what are high order functions? High order functions is a function that takes a function as an argument, then returns a function. JavaScript has a lot of built-in high order functions that are very useful, these are map, filter, sort, reduce and bind are just a few to mention. Now you might ask yourself when is a good time to use higher-order functions. They are useful when you want to customize the behavior of a certain function or you want to use a function to do the same thing over and over. Using these functions will also make your code cleaner, easier to read, and have fewer bugs.

.Map()

The map method returns a new array, where each element in the array is from the result of the corresponding element in the old array. As you will see in the example below the map method takes a function as its first parameter and has three arguments. Example below.

.Filter()

The filter method also creates a new array as output and does what it says it filters given the conditions you set, it will check the condition and if it passes the condition it gets sent to the new array. Below is an example of the syntax.

Now that you see the syntax I will show you a real life example to give you a better idea.

.Sort()

The sort method allows us to sort elements of an array and returns a new array that is sorted in order from small to big by default. Below is an example.

.Reduce()

The reduce method is useful when you have an array of number that you want to add together. Like the following example.

Conclusion

These high-order functions will help with your programming making it easier because the loop that you want to write is already written it will help make your code cleaner have better functionality and quicker for you to write. With a little practice, you will become an expert using these functions.

--

--