Higher Order Functions
MediumIn plain terms
HOFs are functions that take or return functions. map, filter, reduce are HOFs. Enable composition and abstraction. Core to functional programming in JavaScript.
What you need to know
- •Take or return functions
- •map, filter, reduce
- •Composition
Try it yourself
Copy the code below and run it in your browser console or a code editor:
const numbers = [1, 2, 3, 4];
numbers.map(x => x * 2); // [2, 4, 6, 8]
numbers.filter(x => x > 2); // [3, 4]
numbers.reduce((a, b) => a + b, 0); // 10
function withLogging(fn) {
return (...args) => {
console.log('Calling with', args);
return fn(...args);
};
}Learn more
Dive deeper with these trusted resources: