What is the definition of a higher-order function in JavaScript?
Answer
A higher-order function in JavaScript is a function that either:
- Takes one or more functions as arguments (callbacks), or
- Returns a function as its result.
Higher-order functions allow for more dynamic and reusable code by enabling functional programming patterns.
Examples of Higher-Order Functions
- Taking a Function as an Argument:
Skopiuj kod
function repeatOperation(operation, times) {
for (let i = 0; i < times; i++) {
operation();
}
}
repeatOperation(() => console.log("Hello"), 3);
// Output: Hello (3 times)
- Returning a Function:
Skopiuj kod
function multiplier(factor) {
return function(number) {
return number * factor;
};
}
const double = multiplier(2);
console.log(double(5)); // 10
- Built-in Higher-Order Functions: JavaScript includes several built-in higher-order functions like
map
,filter
, andreduce
.
const numbers = [1, 2, 3];
const doubled = numbers.map((num) => num * 2);
console.log(doubled); // [2, 4, 6]
Why Use Higher-Order Functions?
Reusability
: Abstracts common patterns (e.g., iteration or transformations).Composability
: Facilitates chaining and functional-style programming.Cleaner Code
: Reduces boilerplate and makes code more declarative.