Pipeline Operator

Assume we have the following functions:

const repeat = str => `${str}, ${str}`;
const capitalize = str => str[0].toUpperCase() + str.substring(1);
const exclaim = str => `${str}!`;

To apply all of these functions to a single string we need to:

const newString = exclaim(capitalize(repeat("yo")));
document.getElementById('string-1').innerText = newString;

Result:

With the pipeline operator we can now do it like this:

const pipedString = "yo"
	|> repeat
	|> capitalize
	|> exclaim;

document.getElementById('string-2').innerText = pipedString;

Result:

← back to htmlstack.com source code →