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.

  • Adding a rounding line to forEmail needs braces, because the line is a statement and only a body holds statements. Math.round() takes an exact half up, where Python’s round() 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 a def without return gives None.
  • In Python, only a lambda returns without return, and a lambda can’t hold the rounding line, so for_email() stays a def with its return. 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 needs return.
  • Watch out when an arrow returns an object: () => { total: 0 } returns undefined, because the braces read as a body. () => ({ total: 0 }) returns the object, as Python’s lambda: {"total": 0} returns a dict.

Here’s the fixed forEmail, with return in its body:

Language: JavaScript
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`);

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:00 In the last lesson, her Python def for_email became this one-line arrow. Now she wants each amount rounded to the whole euro first.

00:09 Today’s fees are whole euros, so the line it prints shouldn’t change. In this lesson, you’ll see how braces change what the arrow returns.

00:20 The rounding line is a statement, so the arrow needs braces to hold it. The line goes in first, with Math.round(), which rounds a half up, where Python’s round() goes to even.

00:33 The format line slides into the body as it was. Does the arrow still hand it back?

00:41 Look at the braced forEmail. What does the Bauer Legal line print now? Your turn: pause the video and predict.

00:54 Run it, and the line reads undefined, with no error. The Nordwind line, from the one-line forSheet, is unchanged.

01:05 The braces make a body, and a body hands back only what a return gives it. The format line runs, but its text is thrown away, so the call gives undefined, as a def without return gives None in Python.

01:20 In Python, only a lambda returns a value by itself, and a lambda can’t hold this line, so the line goes into her def, which kept its return all along.

01:31 An arrow takes both forms, so nothing reminds you.

01:37 Fix it as Python has it: put return in front of the format line. Run it, and the Bauer Legal line shows its euros again. Or keep the arrow one expression.

01:50 The rounding moves inside the format call, and the same line prints.

01:55 That’s the rule: no braces after the arrow, and the expression is returned, an implicit return. Braces make a block body, which needs a return.

02:07 Watch out with an object, like an empty tally of the day’s quotes, which a Python lambda returns as a dict: JavaScript reads bare braces as a body and returns undefined, so wrap the object in parentheses.

02:22 Both forms are fine. JavaScript developers write one expression while it reads clearly, like forSheet, and use braces for statements or a named step.

02:33 Today’s fees are whole euros, so the next lessons go back to the one-line forEmail, without rounding. Next up is the text lesson Passing Functions Around.

Become a Member to join the conversation.