Understanding First-class and Higher Order Functionsby@Iggy
1,238 reads

Understanding First-class and Higher Order Functions

November 13th 2022
3 min
by @Iggy 1,238 reads
tldt arrow
EN
Read on Terminal Reader

Too Long; Didn't Read

A first-class function is a feature that a programming language or programming language has it’s a function that receives another function as an argument. The main difference between higher-order functions is that a function is called after it has been called after the run. In Javascript, we can store a function in a variable or even property, as you would do to average Javascript values. This opens a new way to write more flexible code since we can. store functions in a. variable or add properties.
featured image - Understanding First-class and Higher Order Functions
Ignatius Sani HackerNoon profile picture

@Iggy

Ignatius Sani

About @Iggy
LEARN MORE ABOUT @IGGY'S EXPERTISE AND PLACE ON THE INTERNET.
react to story with heart

Hello everyone! Today I’m going to be discussing a very famous concept in programming (first-class functions). There are so many resources on this topic already, but there’s still something new to take from this article, and I’ve taken the time to simplify things for better understanding.

Why is this important to me as a developer?

  • It helps you to have a better understanding of Javascript callback functions when you see them used.
  • It helps you to know the behavior of your code.
  • It gives you a better understanding of why Javascript treats functions as values.

What is a First-class function?

According to Wikipedia, a programming language is said to have first-class functions if it treats functions as first-class citizens.

This means the language supports passing functions as arguments to other functions, returning them as the values from different functions, assigning them to variables, or storing them in data structures.

In Javascript, things work this way because a function is just another type of object. If you are new to this concept, you’d feel it’s pretty awkward, especially if you’re coming from a different programming background, but for now, wrap your mind around that. Since objects are values,  functions are too, and  Javascript treats them as one. This opens a new way to write more flexible code since we can store functions in a variable or add properties.

I’ve broken down the main points into a bullet list to make things easier. To better understand first-class functions, here are a few points to clarify it.

  • Functions as an argument
  • Functions returning functions
  • Functions as values

Functions as an argument

One thing made possible with a first-class function is passing a function as an argument; we can see this style of code in a lot of Javascript code. E.g. the addEventListener method, which receives a Javascript callback function as an argument e.g

button.addEventListener(‘click’, function(){
  console.log(‘hellloooo’)
})


In the preceding code snippet, we observe that addEventListener receives two arguments, where the second function is the callback function, and would be called after the button had been clicked.

Functions returning functions

We can return functions from another function in Javascript; many objects in Javascript have methods e.g, Array functions in javascript. Also, we have the function’s method, e.g, the call(), bind(), apply() Javascript methods can all be called on the Javascript functions.

function count(){
  Let counter = 0;
  return function(){
     counter++
   }
}

The count function returned another function where we updated the counter.

Functions as values

In Javascript, we can store a function in a variable or even property, as you would do to average Javascript values.

Const multiply =  (a,b) => a * b

The above Javascript arrow function is stored in the multiply variable, which can be called

multiply(3,5) this way.

Importance of first-class functions in Javascript

One of the primary significance of first-class functions is that they make it possible for us to write higher-order functions.


Higher-Order Function

This is either a function that receives another function as an argument or a function that returns a new function. This is only possible because of the first-class function.

button.addEventListener(‘click’, function(){
  console.log(‘hellloooo’)
})


HereaddEventListener() is an example of a higher-order function because it receives another function as an argument. It often refers to a Javascript callback function because it’s been called after the HOF has run. The main difference between higher-order and first-class functions is that a first-class function is a feature that a programming language has or doesn’t have, it simply means all functions are values.

Conclusion

In conclusion, it is essential to know there is no first-class function in practice; it's just a concept. There are, however, higher-order functions in practice that are possible because the language supports first-class functions.

RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa