what is higher order functions in javaScript ?

Umar Farooque Khan
3 min readJun 10, 2023

--

Higher-order functions in JavaScript are functions that can take other functions as arguments and/or return functions as their results. They are a fundamental concept in functional programming and provide a powerful and flexible way to manipulate and work with functions. This article will explain higher-order functions in JavaScript and provide examples to illustrate their usage.

In JavaScript, functions are treated as first-class citizens, which means they can be assigned to variables, passed as arguments to other functions, and returned as values from functions. This enables the creation of higher-order functions.

A higher-order function can be defined in two ways:

  1. A function that takes a function as an argument:
Higher-order functions

In this example, higherOrderFunction is a higher-order function that takes callback as an argument. callback is expected to be a function that will be invoked within higherOrderFunction.

  1. A function that returns a function:

In this case, higherOrderFunction is a higher-order function that returns a new function. The returned function can be stored in a variable and invoked later.

Higher-order functions offer several benefits in JavaScript development. They allow for the encapsulation of common patterns and logic, promoting code re usability and modularity. They enable the creation of abstract functions that can be customised with different behaviours by providing different callback functions.

Let’s explore a few examples of higher-order functions in JavaScript:

1.map: The map function applies a provided function to each element of an array and returns a new array with the results.

Map function

In this example, the map function takes an array of numbers and applies the callback function (num) => num * num to square each element. It returns a new array [1, 4, 9, 16, 25].

2. filter: The filter function creates a new array with all elements that pass a certain condition specified by a provided function.

Filter function

In this example, the filter function takes an array of numbers and uses the callback function (num) => num % 2 === 0 to filter out only the even numbers. It returns a new array [2, 4].

3.forEach: The forEach function iterates over an array and performs a specified action on each element.

forEach function

In this example, the forEach function takes an array of names and uses the callback function (name) => console.log(Hello, ${name}!) to print a greeting for each name.

Higher-order functions provide a flexible and expressive way to work with functions in JavaScript. They empower developers to create reusable, abstract functions that can be customized with different behaviors by providing different callbacks. By understanding and utilizing higher-order functions effectively, developers can write cleaner, more modular, and more maintainable code.

--

--

Umar Farooque Khan
Umar Farooque Khan

Written by Umar Farooque Khan

Experienced software developer with a passion for clean code and problem-solving. Full-stack expertise in web development. Lifelong learner and team player.

No responses yet