Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Last active April 16, 2024 06:29
Show Gist options
  • Save wilmoore/7d419addace63615fda1ec1794150e49 to your computer and use it in GitHub Desktop.
Save wilmoore/7d419addace63615fda1ec1794150e49 to your computer and use it in GitHub Desktop.
Software Engineering :: Programming :: Languages :: JavaScript :: Pipe Operator

Software Engineering :: Programming :: Languages :: JavaScript :: Pipe Operator

⪼ Made with 💜 by Polyglot.

About

The Pipe Operator (|>) is a feature that aims to introduce a more concise and readable way to chain function calls in JavaScript. It takes the value on its left-hand side and passes it as the first argument to the function on its right-hand side, creating a "pipeline" of operations.

The operator is chaining multiple function calls together, creating a "pipeline" of operations. This can be used in endless real life scenarios especially in algorithmic workflows and data processing.

const numbers = [1, 2, 3, 4, 5];

const doubledAndFiltered = numbers
  |> ((arr) => arr.map((n) => n * 2))
  |> ((arr) => arr.filter((n) => n > 6));
  
console.log(doubledAndFiltered);

Support

As of January 10, 2024, the pipeline operator is not yet a part of the official JavaScript language. But for now you can use a transpiler like Babel so the current JavaScript engines can understand this experimental feature.

Install the Babel plugin
npm install --save-dev @babel/plugin-proposal-pipeline-operator
Configure Babel to use the plugin

babel.config.json

{
  "plugins": ["@babel/plugin-proposal-pipeline-operator"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment