Advertisement
A higher-order function is a function that either accepts another function as an argument, returns a function as its result, or both. This concept is a core part of JavaScript's functional programming capabilities and is widely used for creating modular, reusable, and expressive code.
The syntactic structure of higher order function will be explained with an example as follows,
1// First-order function (does not accept or return another function)
2
3const firstOrderFunc = () =>
4 console.log("Hello, I am a first-order function");
5
6// Higher-order function (accepts a function as an argument)
7
8const higherOrder = (callback) => callback();
9
10// Passing the first-order function to the higher-order function
11
12higherOrder(firstOrderFunc);In this example:
1. firstOrderFunc is a regular (first-order) function.
2. higherOrder is a higher-order function because it takes another function as an argument.
3. firstOrderFunc is also called a callback function because it is passed to and executed by another function.
Advertisement
JavaScript Coding Exercise 14
Test your knowledge with this interactive coding challenge.
Start CodingAdvertisement