Open In App

JavaScript Pipeline Operator

The JavaScript Pipeline Operator ( |> ) is used to pipe the value of an expression into a function. This operator makes chained functions more readable. This function is called the ( |> ) operator and whatever value is used on the pipeline operator is passed as an argument to the function. The functions are placed in the order in which they operate on the argument.

Syntax:

expression |> function

Using the Pipeline Operator

As the Pipeline Operator is an experimental feature and currently in stage 1 proposal, there is no support for currently available browsers and therefore is also not included in Node. However, one can use Babel (JavaScript Compiler) to use it.



Steps:

Example: This exmaple shows the use of Pipeline Operator.




// JavaScript Code (main.js)
function add(x) {
    return x + 10;
}
function subtract(x) {
    return x - 5;
}
// Without pipeline operator
let val1 = add(subtract(add(subtract(10))));
console.log(val1);
 
// Using pipeline operator
 
// First 10 is passed as argument to subtract
// function then returned value is passed to
// add function then value we get is passed to
// subtract and then the value we get is again
// passed to add function
let val2 = 10 |> subtract |> add |> subtract |> add;
console.log(val2);

Output:



20
20
Article Tags :