JavaScript Pipeline Operator (|>): The Hack Pipes Proposal

The Stage 2 pipeline operator passes a value through a chain of calls left to right, with % marking where it goes, instead of nesting h(g(f(x))).

Trevor I. Lasn Trevor I. Lasn
Updated 4 min read

The pipeline operator |> is a proposed JavaScript feature that lets you chain operations in a more readable way. Instead of nesting function calls inside each other, you can write them left-to-right. This matters because deeply nested function calls are hard to read and a common source of bugs.

When you have to read code like h(g(f(x))), your brain has to work backwards from the inside out to understand what’s happening. The pipeline operator lets the code flow naturally from left to right, just like how we read.

The current TC39 proposal (Stage 2) uses Hack pipes, where % is a topic token representing the value from the previous step. This means you write the function call explicitly, placing % where the piped value should go.

How the Pipeline Operator Works

The pipeline operator takes a value on the left and passes it into the expression on the right, where % represents that value:

Each step in the pipeline evaluates the expression on the right with % bound to the result of the left side. If any step throws an error, the pipeline stops there.

A Practical Example

Processing text is a common task that often requires multiple transformations. Think of validating user input, cleaning content for a CMS, or normalizing data for a database. The pipeline operator shines in these scenarios:

The pipeline version shows exactly what happens to the data at each step. You can read it like a recipe: take the name, sanitize it, trim it, then capitalize it. The % topic token makes it explicit where the value goes — and it also works with multi-argument functions like name |> sanitize(%, 'strict'). If another developer needs to add a step (like checking for profanity or validating length), they can simply add a new line to the pipeline without restructuring nested function calls.

Current Status

The pipeline operator is a Stage 2 proposal, which means:

  • It’s not part of JavaScript yet
  • You need Babel to use it
  • The syntax might change

To use it with Babel:


When to Use The Pipeline Operator

The pipeline operator excels at handling data transformations - when you need to process data through multiple steps. Think of functions that each take one main argument and return transformed data for the next step.

This approach makes testing straightforward - each function can be verified in isolation. You can check that calculateTotal works correctly without worrying about validation or tax calculations. When something goes wrong, you can quickly identify which transformation caused the issue since the data flows in a clear, trackable sequence.

Need to add a new step like fraud detection? Just add another function to the pipeline. No need to dig through nested function calls. Each function is also a standalone unit that can be reused in other contexts. Maybe you need to calculateTotal for a shopping cart preview, or formatForSaving for a draft order.

The Hack pipes variant was chosen over the earlier “minimal” (F#-style) proposal because the topic token % works naturally with multi-argument functions, method calls, and arbitrary expressions — not just unary functions.

The pipeline operator isn’t just about making code prettier - it’s about making it more maintainable, testable, and easier to reason about. When each transformation is a clear, single-purpose function, you build a toolkit of reliable operations that can be combined in different ways.


Trevor I. Lasn

Trevor I. Lasn

Building whiteheap.com and 0xinsider.com.



Related Articles

Check out these related articles that might be useful for you. They cover similar topics and provide additional insights.

JavaScript
6 min read

Closures are essential for creating functions that maintain state, without relying on global variables.

Sep 6, 2024
Read article
JavaScript
7 min read

JavaScript has eight falsy values: false, 0, -0, 0n, an empty string, null, undefined and NaN. Empty arrays and objects are truthy. The edge cases, explained.

Oct 27, 2024
Read article
JavaScript
6 min read

Peaks.js is a client-side JavaScript component to display and interact with audio waveforms in the browser

Oct 22, 2019
Read article
JavaScript
4 min read

Intermediate variables name each step of complex logic, and early returns handle failing cases first. A JavaScript authentication check, rewritten both ways.

Aug 30, 2024
Read article
JavaScript
3 min read

x ??= y assigns y only when x is null or undefined, so 0, false and empty strings stay. How it differs from || and where it saves code.

Nov 5, 2024
Read article
JavaScript
8 min read

How to handle unique values and key-value pairs properly without type coercion and performance issues

Nov 17, 2024
Read article
JavaScript
4 min read

Stop writing manual duration formatting code. Instead, leverage the new powerful Intl.DurationFormat API for internationalized time displays

Mar 13, 2025
Read article
JavaScript
3 min read

Use the &&= operator to safely update truthy values while preserving falsy states

Nov 5, 2024
Read article
JavaScript
7 min read

How was JavaScript written back in the day? I dug into early frameworks and libraries to see how code looked then and what still holds up.

Jun 12, 2025
Read article

This article was originally published on https://www.trevorlasn.com/blog/javascript-pipeline-operator. It was written by a human and polished using grammar tools for clarity.