Naming Small Functions With Arrows

The translator has fees and turnaround times. Now she needs quote lines for client emails and her bookkeeping sheet. The Python functions for_email() and for_sheet() each use a def with one return expression. You’ll port them as small, named JavaScript functions.

Name the Formatters

In Python, PEP 8 favors def over assigning a lambda to a name, and a lambda holds only one expression. A JavaScript arrow function, written with =>, is a full function with concise syntax. It can take any number of parameters and, with braces, hold a whole block of statements, so naming one in a const is everyday style. The parameters go before the arrow, and the expression after it supplies the return value:

Language: JavaScript
const euroFormat = new Intl.NumberFormat("en-US", {
  style: "currency",
  currency: "EUR",
});

const forEmail = (amount) => euroFormat.format(amount);
const forSheet = (amount) => amount.toFixed(2);

console.log(`Bauer Legal: ${forEmail(1116)}, 2 working days`);
console.log(`Nordwind GmbH: ${forSheet(1128)}, 6 working days`);

Run it to produce both quote lines:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.