The JavaScript path operator ( |> ) is used to pass the value of an expression to a function. This operator makes string functions easier to read. This function is called the operator ( |> ), and any value used on the pipe operator is passed as an argument to the function. Functions are placed in the order they operate on arguments.

Syntax:

expression |> function


Please Check all JavaScript articles HERE. For more such post you can refer HERE.



Using the Pipeline Operator: Since the Pipeline Operator is an experimental feature and currently in a phase 1 proposal, there is no support for existing browsers and is therefore 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 the package.json file containing relevant information about the project and its dependencies.
    npm init
    
  • Install Babel to use the operator. The path operator is not currently part of the JavaScript language, babel is used to compile code to 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 package.json file which will run babel:
    "start" : "babel main.js --out-file output.js && node output.js"
    
  • Run the code:
    npm start

Example:

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 return value is passed to
//add function then the value we get is passed to
//subtract and then value what we get is again
// passed to add function
let val2 = 10 |> subtract |> add |> subtract |> add;
console.log(val2);

Output:

20
20
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, codeconfig.in Courses are your key to success. We provide top-quality content at no cost, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Explore More

How to Center a Popup Window on Screen

JavaScript window.open() method is used to open a popup window. This popup window will be placed in the center of the screen. This example creates the pop-up window without placing it into

How to hide URL in the popup window opened using window.open

If you are using below code to open a popup window in your web page, then address must be appearing in you pop up window and f you want to

window.showModalDialog is deprecated in Edge and Chrome

If you have a website being compatible with Edge/Chrome and in past it was only compatible with IE 11 and if you are using window.showModalDailog in your JavaScript code, then