Functional Programming in JavaScript: Compose and Pipe Explained

JUSTIN LICARI
The Lock and Key
Published in
8 min readAug 23, 2020

--

Most JavaScript developers are comfortable with the Object Oriented Programming paradigm. However, many problems in JavaScript are more easily solved with the less-known Functional Programming paradigm. In this article, we’ll explore a powerful functional programming technique called Function Composition . We’ll take a look at how to implement a Compose function and a Pipe function, and explain when to use them!

What’s Functional Programming?

Functional programming is a paradigm where we have 2 distinct components to our program — data, and actions we’d like to perform with the data (functions). Functions are logically separate from data. Data passes through the functions, and the functions return new data.

Functional programming is declarative. We define what something is (for example, the sum of two numbers x and y is x + y). Functions take input, and return values. We strive to make functions pure; that is, functions:

  1. accept an input
  2. return an output

And that’s it!

We don’t mutate an application’s global state, or modify data outside of a function.

Examples of Non-Pure Functions

let total = 0// Modifies the global application state (total), and doesn't have a // return value
function inc() {
total++ // side-effect
}

--

--