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.
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.
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.
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.
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.
One of the primary significance of first-class functions is that they make it possible for us to write higher-order functions.
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.
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.