Higher Order Functions in less than five minutes !

What are higher-order functions ?

Higher-order functions simply put are functions that take in a function as an argument(referred to as callbacks) or functions that return a function. Let's split that up into the two parts and give examples.

Taking in a function as an argument(callbacks)

Higher order functions commonly take in functions as arguments. Callbacks simply refers to any function that is passed in as an argument. Typical examples are: event listeners and ES6+ array methods.

//EVENT LISTENERS
window.AddEventListener('click', ()=> {
console.log('hello')
});
// ES6+ ARRAY FUNCTIONS
const array = ['John','Mark','David'].map(el=> console.log(el));

Returning a function

Higher order functions can also return a function, and a prime example of this would be a Redux Thunk action creator. Don't worry about what exactly Redux Thunk is, as it is something from the React/Redux world. It's primary purpose is to serve as a real-world example.

//THUNK ACTION CREATOR 
const dummyFunction = () => {
return dispatch => {
console.log("Successfully returned a function inside another function");
}
};

And that's all you need to know about higher order functions. You will use them a lot I promise you as a lot of work we do in JavaScript revolves around arrays. I will write a follow-up article on some of the most important array methods to master. Goodbye until then.