liveBook · Manning

Pipe operator

As explained in The Joy of JavaScript

A syntax that forwards a value through a sequence of operations, commonly used in functional programming languages.

The pipe operator forwards a value through functions from left to right, so each operation receives the previous operation’s output. It makes function pipelines read in the same direction as data flow, avoiding compose’s reverse order and supporting declarative, point-free JavaScript. section 4.8 section 1.3

Use the pipe model to organize composable operations:

  • Decompose complex behavior into small functions, then connect their outputs into a readable information pipeline. section 1.1 section 4.0
  • Prefer pipe when execution should read naturally from the initial value toward the final result; use compose when right-to-left flow is acceptable. section 4.8
  • Treat |> as native pipeline syntax that removes the need for combinator helpers, while recognizing its described status as a future JavaScript feature. section 4.8 section 1.1

A left-to-right transformation

The expression '1 2 3' |> split |> count first splits the string and then counts the resulting array, producing 3. A final lambda can continue the chain: '1 2 3' |> split |> count |> (x => x ** 2) produces 9. section 1.3 section 4.8

ledger 
 |> Array.from 
 |> filter(b => (b |> prop('isGenesis') |> not)) 
 |> flatMap(prop('data')) 
 |> map(balanceOf(address)) 
 |> reduce(Money.sum, Money.zero()) 
 |> Money.round;

Inspect a longer ledger pipeline that converts data, filters records, maps balances, reduces totals, and rounds the result. section 4.8

How The Joy of JavaScript covers it

How deep it goes
Explains left-to-right flow, helper construction, lambdas, and longer chains while treating |> as a future proposal rather than a settled standard.
Examples
Uses string splitting, counting, lambdas, ledger transformations, array operations, and observable processing as pipeline examples.
What it assumes
Assumes familiarity with functions and composition, while developing decomposition, higher-order functions, currying, purity, and immutability.
What you can do after
Apply it to express sequential transformations clearly and compare natural left-to-right pipelines with composition-based alternatives.
How it connects
Connects pipeline syntax with composition, currying, higher-order functions, pure functions, point-free programming, and observables.

Also explained in

Compare how 6 books explain pipe operator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Joy of JavaScript. The section links point to the parts of the book it draws on.