Sitemap
Ashok Maharjan

Ashok Maharjan

Higher Order Functions in JavaScript

2 min readSep 19, 2020

--

Press enter or click to view image in full size
https://www.teachhub.com/teaching-strategies/2019/10/teaching-strategies-that-enhance-higher-order-thinking/

Higher order function in JavaScript is a function that operate on other function either by taking a function as an argument or by returning a function. Some of these functions are built in with JavaScript array and we will explore few of them today.

Map

The map() method produces a new array after applying some functions to the each individual elements of the original array. Let’s look at the following example where we use map() to raise the power of each element by 3.

Reduce

The reduce() method is similar to map in a way because it apply some functions to each elements of the original array but mostly is used to keep a running total of the operation and optionally has an “initial value”. It returns a value rather than a new array. In the example below we have used reduce() method to return the running total of all elements of an array. In the second example we have added an optional initial value of 5, so the returned value is total value + initial value.

Filter

The filter() method return a new array by filtering all the elements that does not pass some test. In the following example of the filter(), we are filtering the array with only the words that starts with letter ‘a’.

One important point to note is that original array would remain the same even after any of these methods have been applied to them. These are just a few examples of a higher order function that are already built in but they are a good starting point.

Get Ashok Maharjan’s stories in your inbox

Join Medium for free to get updates from this writer.

That’s all for this edition of my blog, until next edition happy coding!

--

--