Compile Ready
Module 5 · Functions

First-Class Functions

Intermediate9m read12m practice21m total
First-Class FunctionsClosuresCompositionFunction Values

Introduction

JavaScript has first-class functions, which means functions are values. You can store them in variables, put them in arrays or objects, pass them to other functions, and return them from functions.

Higher-order functions are a pattern; first-class functions are the language capability that makes the pattern possible.

Why This Matters

First-class functions are the reason JavaScript can express callbacks, closures, dependency injection, strategies, middleware, and composition so naturally. Interviewers expect you to distinguish the language feature from higher-order function usage.

Theory

What first-class means

A value is first-class if the language treats it like any other value. In JavaScript, functions can be assigned to variables, stored in arrays, stored in object properties, passed as arguments, returned from other functions, and compared by reference identity.

Every function expression creates a function object. Two functions with identical source are still different objects. Assigning a function to another variable copies the reference to the same function object.

When a function returns another function, the inner function can keep using variables from the outer call even after the outer call has finished. This is how factories like makeAdder(10) work. Storing functions in an object also lets you choose behavior by key instead of writing long conditional chains; this is common in validators, formatters, reducers, and command handlers.

Because functions are values, you can build new functions by combining smaller ones. Helpers like pipe and compose are interview staples because they test first-class functions, higher-order functions, closures, rest parameters, and reduction all at once.

Visual Diagrams

Functions move like values
function object
   |   |  assigned to variable
   |   stored in object or array
   |    passed as argument
   |     returned from another function
   v
same callable value can travel through the program

First-class functions let behavior be stored, selected, passed, and returned.

Code Examples

Store strategies in an object

Selecting a function by key is often cleaner than a long conditional chain.

Loading…

Return a specialized function

The returned function closes over factor, even after makeMultiplier has returned.

Loading…

Playground

Loading editor…
Console

Press Run to execute the code and see output here.

Output Prediction

Predict the output #1

javascript
1function makeAdder(base) {
2 return function (value) {
3 return base + value;
4 };
5}
6
7const add10 = makeAdder(10);
8const add20 = makeAdder(20);
9const list = [add10, add20];
10
11console.log(add10(1));
12console.log(add20(1));
13console.log(list[0](5) + list[1](5));

Coding Exercises

Implement pipe and compose

Hard

Implement pipe(...fns) and compose(...fns). pipe(a, b, c)(value) should run left to right: c(b(a(value))). compose(a, b, c)(value) should run right to left: a(b(c(value))). Assume each function accepts one value and returns one value.

Interview Questions

1What does it mean that functions are first-class in JavaScript?

It means functions are values. They can be assigned to variables, stored in arrays or objects, passed as arguments, returned from other functions, and compared by reference. This capability enables callbacks, higher-order functions, closures, composition, and strategy patterns.

Asked at:GoogleMetaAmazonMicrosoft

Follow-ups

  • How is first-class function support different from a higher-order function?
  • Why does returning a function often create a closure?
2What is the difference between first-class functions and higher-order functions?

First-class functions are a language feature: functions can be treated as values. A higher-order function is a function that uses that feature by accepting or returning a function. JavaScript supports first-class functions, so we can write higher-order functions.

Quiz

1. Which example demonstrates first-class functions most directly?

Summary

  • First-class functions means functions are values in JavaScript.
  • Functions can be assigned, stored, passed, returned, and compared by reference.
  • Returning functions commonly creates closures over outer variables.
  • Composition helpers like `pipe` and `compose` rely on first-class functions.

Cheat Sheet

First-class: functions are values.

Can be: assigned, stored, passed, returned, compared by reference.

Enables: callbacks, HOFs, closures, strategies, middleware, composition.

HOF vs first-class: first-class is the capability; HOF is a function that accepts or returns functions.

Composition: pipe(f, g)(x) = g(f(x)); compose(f, g)(x) = f(g(x)).