What are Higher-order functions (HOF)? The benefits of using them in your projects.

What are Higher-order functions (HOF)? The benefits of using them in your projects.

Daily short news for you
  • When researching a particular issue, how do people usually take notes? Like documents found, images, links, notes...

    I often research a specific topic. For example, if I come across an interesting image, I save it to my computer; documents are similar, and links are saved in my browser's Bookmarks... But when I try to find them later, I have no idea where everything I saved is, or how to search for it. Sometimes I even forget everything I've done before, and when I look back, it feels like it's all brand new 😃.

    So I'm nurturing a plan to build a storage space for everything I learn, not just for myself but also with the hope of sharing it with others. This would be a place to contain research topics, each consisting of many interconnected notes that create a complete notebook. Easy to follow, easy to write, and easy to look up...

    I write a blog, and the challenge of writing lies in the writing style and the content I want to convey. Poor writing can hinder the reader, and convoluted content can strip the soul from the piece. Many writers want to add side information to reinforce understanding, but this inadvertently makes the writing long-winded, rambling, and unfocused on the main content.

    Notebooks are created to address this issue. There's no need for overly polished writing; instead, focus on the research process, expressed through multiple short articles linked to each other. Additionally, related documents can also be saved.

    That’s the plan; I know many of you have your own note-taking methods. Therefore, I hope to receive insights from everyone. Thank you.

    » Read more
  • altcha-org/altcha is an open-source project that serves as an alternative to reCaptcha or hCaptcha.

    Studying these projects is quite interesting, as it allows you to learn how they work and how they prevent "spam" behavior 🤓

    » Read more
  • Manus has officially opened its doors to all users. For those who don't know, this is a reporting tool (making waves) similar to OpenAI's Deep Research. Each day, you get 300 free Credits for research. Each research session consumes Credits depending on the complexity of the request. Oh, and they seem to have a program giving away free Credits. I personally saw 2000 when I logged in.

    I tried it out and compared it with the same command I used before on Deep Research, and the content was completely different. Manus reports more like writing essays compared to OpenAI, which uses bullet points and tables.

    Oh, after signing up, you have to enter your phone number for verification; if there's an error, just wait until the next day and try again.

    » Read more

The Problem

I still remember when I first read an article about Higher-order functions, there was a certain level of confusion. Being new to the field, the theoretical aspect was quite difficult to grasp. At that time, I understood the concept but couldn't visualize its practical applications. It took me a while to realize that this was something I had been using regularly all along!

There are already many articles about Higher-order functions on the internet, just search on Google and you will find them. However, because HOF is an important and highly applicable concept, I would like to write an article from a personal perspective. Without further ado, let's get started.

What are HOF?

Higher-order functions in JavaScript are functions that can take one or more functions as arguments or return another function.

For example, a function can be passed as an argument to another function, or a function can return another function to be used in our code. This allows us to create flexible and reusable functions.

In JavaScript, functions like map, filter, reduce... are typical examples of HOF. They all take a function as an argument and apply that function to an array or other values to perform a certain transformation.

How to use them

You can apply HOF to create flexible functions of your own or create reusable functions.

One of the common use cases for HOF is in functions like map, filter, reduce. If you are not familiar with the usefulness of these three functions, I have a detailed article on Why you should be proficient with the map, filter, and reduce trio in JavaScript?.

Back to the problem, let's say we have an array and we need to apply an operation to all elements in it, then we use map.

const arr = [1, 2, 3];
const arr2 = arr.map(function (item) {
  return item + 1;
});

map is an HOF because it takes a function as a parameter. As the parameter is a function, we can rewrite the above code in a reusable way like this:

const addOne = a => a + 1;

const arr = [1, 2, 3];
const arr2 = arr.map(addOne);

addOne is a function that takes a number a and returns a + 1. Thus, we can use addOne in multiple map functions without the need to rewrite the addition logic.

Benefits of HOF

HOF brings many benefits in programming:

  • Reusability: HOF allows creating flexible and reusable functions to solve various problems without rewriting the same code multiple times. It reduces code duplication.

  • Separation of data and logic, simplifying code writing: HOF allows separating data and logic, making the code more readable and maintainable. In the example above, instead of guessing what map is doing, we can just look at the function name addOne to get an idea.

  • Many HOF functions are created to support programming. Examples include map, filter, and many others. They are designed to save coding time, as without them, you would have to write more code to achieve the same purpose. In addition, there are many HOF functions in asynchronous processing like Promise, async/await, and application libraries that provide flexible processing functions for developers.

Along with the benefits, HOF can be difficult to understand and access for beginners in JavaScript, but this problem can be overcome with time. Additionally, using too many HOF functions can introduce a certain level of complexity in code comprehension and maintenance (each function is an object created), or for future maintenance. However, these weaknesses can be completely overcome with time and experience gained by programmers.

Conclusion

Higher-order functions (HOF) are functions that can take functions or return a function. HOF is widely used in JavaScript programming because of the benefits it provides. However, it can also cause difficulties in understanding for beginners and potential maintenance issues.

Premium
Hello

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...