DEV Community

vidhya murali
vidhya murali

Posted on

JavaScript Higher Order Functions

Higher Order Functions in JavaScript

A higher-order function is a function that does one of the following:

  • Takes another function as an argument.
  • Returns another function as its result.

Higher-order functions help make your code more reusable and modular by allowing you to work with functions like any other value.

fun2 is a higher-order function because it takes another function (action) as an argument.
It calls the action function twice.

Popular Higher Order Functions in JavaScript

  1. map The map function is used to transform an array by applying a callback function to each element. It returns a new array.

  1. filter The filter function is used to create a new array containing elements that satisfy a given condition.

  • The callback (num) => num % 2 === 0 filters out elements not divisible by 2.
  • The resulting array contains only even numbers.
  1. reduce The reduce function accumulates array elements into a single value based on a callback function.

  1. forEach The forEach function executes a provided function once for each array element.

  1. find The find function returns the first element in the array that satisfies a given condition.

  1. some The some function checks if at least one array element satisfies a condition.

  1. every The every function checks if all array elements satisfy a condition.

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

A higher-order function is a function that does one of the following:

A higher order function can also do both of those things - it's not one or the other.