JavaScript | Pipeline Operator - GeeksforGeeks

12 Jun, 2020
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 using ( |> ) 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 the 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 project and its dependencies. npm init

npm init

Install babel in order to use 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 

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"
          }    
      ]
  ]
}

{
  "plugins": 
  [
      [
          "@ babel/plugin-proposal-pipeline-operator",
          {
              "proposal" : "minimal"
          }    
      ]
  ]
}

Add a start script to package.json file which will run babel:"start" : "babel main.js --out-file output.js && node output.js"

"start" : "babel main.js --out-file output.js && node output.js"

Run the code: npm start
npm start
Example:
Javascript
// JavaScript Code (main.js)  function add(x) {    return x + 10;}  function subtract(x) {    return x - 5;}  // Without pipeline operatorlet 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 functionlet val2 = 10 |> subtract |> add |> subtract |> add;console.log(val2);
Output:
20
20
JavaScript-Misc
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
JavaScript | Promises
How to get character array from string in JavaScript?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript