Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Knowing What an Arrow Returns

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.

  • The translator’s formatters are one-line arrows. She wants each email amount rounded to the whole euro first, as a named step that reads clearly, so forEmail needs a rounding line. Today’s fees are whole euros already, so the quote lines shouldn’t change.
  • In Python, for_email() gains rounded = round(amount), and the return line now formats rounded. The script’s two lines print as before.
  • In JavaScript, the rounding line is a statement, and only a body can hold statements, so the arrow gets braces. It uses Math.round() in place of Python’s round(). On an exact half they differ: Python’s round() goes to the even whole number, and Math.round() goes up, to the larger number.
  • The format line moves into the body, now formatting rounded, still with no return. The Bauer Legal line then reads Bauer Legal: undefined, 2 working days, and nothing complains, so the email would go out to the client like that. The Nordwind line, from the one-line forSheet, prints as before.
  • The format line still runs: it works out the euro text, then the text is thrown away, because nothing hands it back to the caller.
  • The braced body ends without a return, so the call gives undefined, where a Python def would give None, and the template literal turns that undefined into text.
  • Python splits the two forms the same way: a lambda returns its one expression with no return keyword, like an arrow without braces, and a def’s body always needs its return line, like an arrow with braces. Where the comparison stops: a lambda can’t grow a body, but an arrow can, so nothing reminds you to add a return when it does.
  • One fix is return euroFormat.format(rounded);. The other keeps the arrow one expression, with no braces: const forEmail = (amount) => euroFormat.format(Math.round(amount));.
  • Watch out when an arrow returns an object, like a fresh empty tally for each day’s quotes, which Python’s lambda: {"total": 0} returns as a dict. Without parentheses, () => { total: 0 } returns undefined, because JavaScript reads the braces as a body. () => ({ total: 0 }) returns the object, because the parentheses make JavaScript read the braces as a value.

Here’s the braced forEmail without its return, the version that returns undefined:

Language: JavaScript
const forEmail = (amount) => {
  const rounded = Math.round(amount);
  euroFormat.format(rounded);
};

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

00:02 The translator’s two formatters are one-line arrows now, ported from the defs in her Python script. Next, she wants each email amount rounded to the whole euro first, as a named step that reads clearly, so forEmail needs a rounding line.

00:18 Today’s fees are whole euros already, so the lines shouldn’t change. In this lesson, you’ll see what that new line does to what the arrow returns, and learn the one rule behind it.

00:30 First, run both files as they stand. They print the same two lines.

00:38 In Python, for_email() gains the rounding line, and the return line now formats the rounded value.

00:46 In the JavaScript port, the rounding line is a statement, and only a body can hold statements, so the arrow gets braces, as a function declaration’s body does.

00:56 The rounding line goes in first, with Math.round() doing the job of Python’s round() here. On an exact half, they differ: Python’s round() goes to the even number, and Math.round() goes up, to the larger number.

01:11 The format line slides down into the body as it was, now with the rounded amount.

01:18 Run both files again. The Python script prints its two lines as before. In JavaScript, the Bauer Legal line reads undefined where the fee should be, and nothing complains, so the email would go out to the client like that.

01:34 The Nordwind line, from the one-line forSheet, is unchanged. Look at the braced forEmail on its own. Does its format line still run?

01:45 Your turn: pause the video and predict.

01:53 It does. It works out the euro text, then the text is thrown away, because nothing hands it back to the caller.

02:03 The braces are the cause. Without them, an arrow’s one expression is what it returns. With them, the arrow gets a body, like a def’s, and a body hands back only what a return statement gives it.

02:17 This body ends with no return, so the call gives undefined, where a Python def would give None, and the template literal turns that undefined into text.

02:28 Python splits the two forms the same way. A lambda returns its one expression with no return keyword, like an arrow without braces.

02:38 A def’s body always needs its return line, like an arrow with braces. Where the comparison stops: a lambda can’t grow a body, but an arrow can, so nothing reminds you to add a return when it does.

02:54 Fix it the way Python already had it: put return in front of the format line. Run the file, and the Bauer Legal line shows its euros again. The other fix keeps the arrow one expression, with no braces.

03:09 The rounding moves inside the format call, and the arrow returns the result, so the same line prints.

03:18 That’s the rule: no braces after the arrow, and the expression is returned, which JavaScript developers call an implicit return. Braces make a block body, and a body needs return to give anything back.

03:33 Watch out when an arrow returns an object, like a fresh empty tally for each day’s quotes, which Python’s lambda returns as a dict: without parentheses, JavaScript reads the braces as a body and returns undefined, so wrap the object in parentheses to make it a value.

03:52 Both versions of forEmail are fine. A JavaScript developer writes one expression by default, while it reads clearly, and switches to braces and return once a function needs statements, or a named step like the rounded amount reads better.

04:06 Next up is Passing Functions Around, a text lesson. There, the quote line takes the formatter as an argument, so each line can pass the one it needs, forEmail for the email and forSheet for the sheet.

Become a Member to join the conversation.