Distinguishing Between First-Class and Higher-Order Functions in JavaScript

Rehmat Sayany
2 min readJul 30, 2023

--

Introduction

When venturing into the world of functional programming in JavaScript, two concepts you’ll frequently encounter are “first-class functions” and “higher-order functions.” While these two ideas are related, they’re not the same thing. To help clarify, this blog post will break down what each term means and how they relate to one another.

First-Class Functions

A programming language is said to have “first-class functions” when it treats functions like any other variable. In a language with first-class functions:

  1. Functions can be assigned to variables.
  2. Functions can be passed as arguments to other functions.
  3. Functions can be returned by other functions.
  4. Functions can be stored in data structures such as arrays or objects.

Essentially, first-class functions can be manipulated in the same ways as any other value in the language. They can be created at runtime, stored in data structures, passed around as arguments, or returned as values from other functions.

JavaScript supports first-class functions. Here’s a simple example where a function is assigned to a variable and then stored in an array:

let greet = function() { console.log("Hello, world!"); } 
let arr = [greet];
arr[0](); // Outputs: "Hello, world!"

Let's take another example where the function is assigned to variable and then stored in an object

const circleProperties = (radius: number[]) => {
const calculate = (fn:fn ) => radius.map(fn);

return {
diameter: () => calculate(radius => 2 * radius),
area: () => calculate(radius => Math.PI * radius * radius),
calculateCircumferences: () => calculate(radius => 2 * Math.PI * radius)
}
}
circleProperties.diameter([1,2.3])

Higher-Order Functions

Higher-order functions are a concept derived from the fact that functions are first-class in a language. A higher-order function does at least one of the following:

  1. Accepts one or more functions as arguments.
  2. Returns a function as its result.

All higher-order functions are made possible because functions are first-class, but not all usages of first-class functions involve higher-order functions. Here’s an example of a higher-order function:

function greetSomeone(greetFunction, name) {
greetFunction(name);
}
let sayHello = function(name) { console.log("Hello, " + name + "!"); }greetSomeone(sayHello, "Alice"); // Outputs: "Hello, Alice!"

In this example, greetSomeone is a higher-order function because it takes another function (sayHello) as an argument.

Summary

To sum up, first-class functions refer to a language’s ability to treat functions like any other variable. You can pass them around, store them in data structures, or have them be output by other functions.

On the other hand, higher-order functions are a direct result of having first-class functions in a language. Higher-order functions are those that take other functions as arguments or output them as a result.

Understanding these concepts is crucial to mastering functional programming in JavaScript. They lay the groundwork for many powerful programming techniques, including callbacks, promises, and closures. By making the most of first-class and higher-order functions, you can write more flexible, efficient, and clean code. So, keep these concepts in mind as you continue your JavaScript journey! Happy coding!

--

--

Rehmat Sayany
Rehmat Sayany

Written by Rehmat Sayany

Full Stack developer @westwing passionate about NodeJS, TypeScript, React JS and AWS.

No responses yet