Skip to content

JavaScript pipe operator ( |> ) | Code

  • by

JavaScript pipe operator ( |> ) allows us to pipe a value through a chain of functions. Basic syntax support for the operator looks like this:

expression |> function

JavaScript pipe operator ( |> )

Simple example code.

You will need the pipeline operator babel plugin (or similar transpiler tool) if you want to run these examples locally.

$ npm install --save-dev @babel/plugin-proposal-pipeline-operator
const double = x => x * 2

const num = 5
const doubled_num = num |> double

console.log(doubled_num)

Output:

JavaScript pipe operator

Source: medium.com

More code

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);

Do comment if you have any doubts or suggestions on this JS Operator topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *