Knowing What an Arrow Returns
In this lesson, you grew a one-line arrow into a body and saw why an arrow with braces needs return.
- Adding a rounding line to
forEmailneeds braces, because the line is a statement and only a body holds statements.Math.round()takes an exact half up, where Python’sround()goes to the even number. - With the format line moved into the body as it was, the Bauer Legal line reads
Bauer Legal: undefined, 2 working days, with no error. The format line still runs, but its text is thrown away, as adefwithoutreturngivesNone. - In Python, only a lambda returns without
return, and a lambda can’t hold the rounding line, sofor_email()stays adefwith itsreturn. An arrow takes both forms, so nothing reminds you. - The rule: no braces after
=>, and the expression is returned (an implicit return). Braces make a block body, and a body needsreturn. - Watch out when an arrow returns an object:
() => { total: 0 }returnsundefined, because the braces read as a body.() => ({ total: 0 })returns the object, as Python’slambda: {"total": 0}returns a dict.
Here’s the fixed forEmail, with return in its body:
const euroFormat = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "EUR",
});
const forEmail = (amount) => {
const rounded = Math.round(amount);
return euroFormat.format(rounded);
};
console.log(`Bauer Legal: ${forEmail(1116)}, 2 working days`);