First Class Functions, Higher Order Functions & Callback Functions

All three of these terms are related and go together. They are as simple to understand as they are widely used in javascript. And they are all part of javascript’s functional programming paradigm.

Here is a quick definition of each type of function, followed by full explanations of each.

A first class function is a function that was built with the intention of being passed around to other functions. It does one specific thing, does not have side effects, and is not intended to be called directly, but rather, to be used by ‘other functions.’

Those ‘other functions’ which accept one of those ‘first class functions’ as an argument are called higher order functions. Higher order functions also might be functions that return a function.

In a higher order function, when one of the parameters passed in is a function, that function is a callback function because it will be called back and used within the higher order function.

A higher order function is named as such because when using a callback to perform an operation within itself, the function has a ‘higher’ purpose than a regular function. When it returns a function, it also has a ‘higher’ purpose.

First Class Functions

First class functions should be created by function expressions, as opposed to function declarations. This makes them easy to read and pass around. Here is an example that would never be used in the real world, but should show this concept in more detail…

The numbers function above is a higher order function that takes in a first class function as its callback function. It is custom built (not built in to js.)

Higher Order Functions

Typically, when beginners learn higher order functions they are learning the Array.prototype functions that javascript now provides as part of the language. Those functions are .map(), .filter(), .every(), .some(), and many more, which work by taking in a function as its parameter and applying it to every item an array.

.map()

The .map() function is a higher order function that will run a callback on every item in the array and will push each result into a new array.

The above example uses an anonymous function as its callback. However, you can also do the same thing by creating and using a first class function.

.filter()

The .filter() function will run a callback function on every item in an array and will return either true or false for each item. If true, it will push the item to the new array.

And done with a first class function as the callback…

Functional Programming Paradigm

You might be asking, why even create a first class function, why not just code an anonymous function as the callback, after all, it’s so simple?

Here is the reason… Because the purpose of functional programming, and coding in general actually, is to write reusable code. Don’t repeat yourself!

So, in the above example for .filter() we wrote a first class function isOdd. Now, we can use isOdd in any other function in the whole program. Let’s keep going with more higher order functions, but now we’ll use the isOdd function.

.every()

The .every() function will run a callback function on every item in the array checking for a returned true or false value. If every item in the array returns true, it will return true. If any item in the array returns false, it will stop its loop and immediately return false.

.some()

The .some() function will run a callback function on every item in an array checking for a returned true or false value. If any item in the array returns true, it will return true. If all of the items in the array returns false it will return false. As soon as it gets one true value it will stop the loop and return true.

In a future articles we will dive even deeper into how Javascript frameworks and libraries use first class functions, higher order functions, and callback functions to create amazingly useful tools for developers. We will also dive into what is literally happening behind the scenes of these functions.

developer. entrepreneur. student.