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 ( |> ) 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:
- 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
Example:
Javascript
function add(x) {
return x + 10;
}
function subtract(x) {
return x - 5;
}
let val1 = add(subtract(add(subtract(10))));
console.log(val1);
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,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, 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!