The Wayback Machine - https://web.archive.org/web/20240618230320/https://www.geeksforgeeks.org/javascript-pipeline-operator/
Open In App

JavaScript Pipeline Operator

Last Updated : 08 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The JavaScript Pipeline Operator (|>) is used for passing the result of one expression into a function. It’s particularly useful for making long chains of functions easier to read. With this operator, the value before it gets sent as input to the function that follows. You simply arrange the functions in the order you want them to act on the input.

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:

  • Before moving ahead, make sure that Node.js is installed.
  • Create a directory on your desktop (say pipeline-operator) and within that directory create a JavaScript file (say main.js).
  • Navigate to the directory and initialize a package.json file that contains relevant information about the project and its dependencies. 
    npm init
  • Install Babel in order to use the operator. A pipeline operator is not currently part of JavaScript language, babel is used to compile the code so that it can be understood by Node.
    npm install @babel/cli @babel/core 
        @babel/plugin-proposal-pipeline-operator 
  • To direct Babel about the compilation of the code properly, a file named .babelrc is created with the following lines:
    {
      "plugins": 
      [
          [
              "@ babel/plugin-proposal-pipeline-operator",
              {
                  "proposal" : "minimal"
              }    
          ]
      ]
    }
  • Add a start script to the package.json file which will run babel:
    "start" : "babel main.js --out-file output.js && node output.js"
  • Run the code: 
    npm start

Example: This exmaple shows the use of Pipeline Operator.

Javascript
// 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

Previous Article
Next Article

Similar Reads

Node.js Stream.pipeline() Method
The stream.pipeline() method is a module method that is used to the pipe by linking the streams passing on errors and accurately cleaning up and providing a callback function when the pipeline is done. Syntax: stream.pipeline(...streams, callback) Parameters: This method accepts two parameters as mentioned above and described below. ...streams: The
3 min read
Underscore.js _.pipeline() Method
The _.pipeline() method takes a list of functions, either as an array or as arguments, and returns a function that takes some value as its argument and runs it through a pipeline of the original functions given in this function. Syntax: _.pipeline( func1, func2,..., funcn ); Parameters: This method accepts a single parameter in the form of n functi
2 min read
Lodash _.pipeline() Method
The Lodash _.pipeline() method takes a list of functions, either as an array or as arguments, and returns a function that takes some value as its argument and runs it through a pipeline of the original functions given in this function. Syntax: _.pipeline( func1, func2,..., funcn ); Parameters: This method accepts a single parameter in the form of n
2 min read
Mongoose Aggregate.prototype.pipeline() API
The Aggregate API.prototype.pipeline() method of the Mongoose API is used to perform aggregation tasks. It allows us to get the current pipeline operation object in the form of an array. It is useful to get all the current pipelining operations or pipeline methods we have applied to perform aggregation tasks. Syntax: aggregate().project( specificat
2 min read
How to Set Up a CI Pipeline for Ktor Using GitHub Actions?
In this article, we'll look at how to use GitHub Actions to create a robust and effective Continuous Integration (CI) pipeline for Ktor applications. Developers have a great foundation to construct high-performance server-side apps thanks to Ktor, a versatile and lightweight Kotlin framework for building web applications. We can automate the build,
6 min read
Ternary operator vs Null coalescing operator in PHP
Ternary Operator Ternary operator is the conditional operator which helps to cut the number of lines in the coding while performing comparisons and conditionals. It is an alternative method of using if else and nested if else statements. The order of execution is from left to right. It is absolutely the best case time saving option. It does produce
3 min read
Why we should prefer square operator over the dot operator to access the value from the object ?
The square notation should be used as much as possible as compared to the dot notation to access the value from the object in JavaScript. Let's first understand how to obtain the value by using the square notation and dot notation from the object. Square notation: In the square notation the keyName that is passed is treated as a variable and search
3 min read
JavaScript Course Conditional Operator in JavaScript
JavaScript Conditional Operators allow us to perform different types of actions according to different conditions. We make use of the 'if' statement. if(expression){ do this; } The above argument named 'expression' is basically a condition that we pass into the 'if' and if it returns 'true' then the code block inside it will be executed otherwise n
3 min read
Left Shift Assignment (<<=) Operator in JavaScript
The Left Shift Assignment Operator is represented by "<<=". This operator moves the specified number of bits to the left and assigns that result to the variable. We can fill the vacated place by 0. The left shift operator treats the integer stored in the variable to the operator's left as a 32-bit binary number. This can also be explained as
2 min read
JavaScript ‘===’ vs ‘==’Comparison Operator
In Javascript(ES6), there are four ways to test equality which are listed below: Using '==' operatorUsing '===' operatorSameValueZero: used mainly in sets, maps and arrays.SameValue: used elsewhereNow, our main concern is getting to know the difference between the '==' and '===' operators that the javascript provides, though they look similar, they
3 min read