DEV Community

Cover image for What is pipeline operator in JavaScript?
NisalK
NisalK

Posted on

What is pipeline operator in JavaScript?

The pipe operator (|>) is a proposed addition to the JavaScript language that would allow for a more concise and readable way of chaining function calls. It is inspired by similar operators in functional programming languages such as Elixir and F#.

The idea behind the pipe operator is to take the output of one function and immediately pass it as the input to the next function in the chain. For example, consider the following code that uses the map, filter, and reduce functions to transform an array of numbers:

const numbers = [1, 2, 3, 4, 5];
const result = numbers
  .map(n => n * 2)
  .filter(n => n > 4)
  .reduce((acc, n) => acc + n, 0);
console.log(result); // 14
Enter fullscreen mode Exit fullscreen mode

With the pipe operator, the same code could be written like this:

const numbers = [1, 2, 3, 4, 5];
const result = numbers 
|> n => n * 2 
|> n => n > 4 
|> (acc, n) => acc + n;
console.log(result); // 14
Enter fullscreen mode Exit fullscreen mode

In this example, the pipe operator takes the numbers array and passes it as the first argument to the first function (n => n * 2), which returns a new array with each element multiplied by 2. This new array is then passed as the first argument to the second function (n => n > 4), which returns a new array containing only the elements that are greater than 4. Finally, this new array is passed as the first argument to the reduce function, which computes the sum of all elements.

The pipe operator can also be used to chain method calls on an object, by providing the object as the first argument to the function. For example:

const obj = {a: 1, b: 2, c: 3};
const result = obj 
|> ({a,b}) => ({a,b}) 
|> ({a,b}) => a + b;
console.log(result); // 3
Enter fullscreen mode Exit fullscreen mode

The pipe operator is not a part of the official JavaScript language yet, but it is a popular proposal and has been implemented in several transpilers and libraries, such as the pipeline operator proposal in the babel.js transpiler.

One of the main advantages of the pipe operator is that it can make code more readable by making it clear the flow of data through a chain of functions. Instead of having to read the code from left to right and keep track of intermediate variables, the flow of data can be followed directly from the first function to the last. This can make it easier to understand the purpose of the code and to find and fix bugs.

Additionally, it's also a good practice to use the pipe operator when dealing with functional programming, it makes the code look more elegant and readable as well.

However, not every developer might find the pipe operator useful, it's a matter of personal preference, and it's important to evaluate the readability and maintainability of the codebase, before making a decision to use the pipe operator or not.

More information πŸ‘‡

I hope you found this article helpful. Connect with me on Dev.to and LinkedIn.com Thank you !

Top comments (1)

Collapse
 
cgrisar profile image
cgrisar

The expected result of the example would be 24, not 14 (6+8+10)