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

Returning Two Results at Once

Returning Two Results at Once

In this lesson, you ported a function that hands back two results and saw why JavaScript needs an array where Python returns a tuple.

  • Python’s quote() works out the fee and the turnaround and hands both back at once with return fee, days. The caller unpacks them with fee, days = quote(6200). For the 6,200-word contract without the rush, the call gives the tuple (744.0, 5).
  • The line-by-line port keeps return fee, days;, and console.log(quote(6200)) logs only 5, with no error: like a missing argument, the slip hides behind a believable number. That’s because JavaScript has no tuples. In Python, the comma is what builds the tuple, even without parentheses. In JavaScript, the comma is an operator: it evaluates the fee and drops it, then evaluates the days and keeps that last value.
  • Parentheses don’t make a tuple either: return (fee, days); still gives 5.
  • Python’s unpacking ports as array destructuring, const [fee, days] = quote(6200);, the same destructuring you can write in a for...of header. With quote() returning a single number, that line throws TypeError: quote is not a function or its return value is not iterable, and the stack trace points to line 8. Unlike a missing argument, this doesn’t go quiet.
  • quote is a function, so it’s the message’s second cause: the return value isn’t iterable. Iterable means what it means in Python, something you can loop over, and a number isn’t. Python stops the same way: fee, days = 5 raises TypeError: cannot unpack non-iterable int object.
  • The real slip is back up in quote()’s return, which ran without a word, so when destructuring fails, read the function’s return first.
  • The fix goes where the results leave the function: return [fee, days];. The first call then logs [ 744, 5 ], and the destructuring unpacks it into the fee and the days. The fee prints as 744, without Python’s .0. JavaScript numbers don’t split into integers and floats, so a whole value prints without one.
  • The array does the tuple’s job here, a pair read by position. Unlike a tuple, nothing stops code from changing the array later.
  • When the results deserve names, where Python might reach for a dict or a named tuple, a function returns an object instead, and the caller reads each one by name. The short way to write that object comes later in this course.

Here’s the Python script’s quote() and its caller:

Language: Python
def quote(words):
    fee = max(words * 0.12, 40)
    days = 2 + words // 2000
    return fee, days

print(quote(6200))
fee, days = quote(6200)
print(fee, 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:02 The turnaround helper is ported, but a quote needs a second number: the fee. In the translator’s Python script, quote() works out both, and return fee, days hands them back at once.

00:15 Ported line by line, that function logs only 5. In this lesson, you’ll see why, and get both results back in JavaScript.

00:25 Run the Python for the law firm’s 6,200-word contract, leaving the rush aside for now. The call gives a tuple: 744.0 and 5. And the caller unpacks it into the fee and the days.

00:42 Here’s the line-by-line port. As in the turnaround helper, def becomes function, and the math moves to the Math.max() and Math.floor() calls. The return line stays as Python wrote it, comma and all.

00:57 Run the first call, which logs whatever quote() returns: only 5, the number from the start. The fee is gone, and nothing complains. Like the missing rush argument, the slip hides behind a believable number.

01:13 That’s because JavaScript has no tuples. In Python, the comma is what builds the tuple, even without parentheses. In JavaScript, the comma is an operator. It evaluates the fee and drops it, then evaluates the days and keeps that last value.

01:31 Parentheses don’t make a tuple either: return (fee, days) still gives 5.

01:39 The caller’s two lines port next. Python’s unpacking becomes array destructuring: const, then the two names in square brackets, the same destructuring you can write in a for...of header.

01:51 But quote() now returns a single number. In the last lesson, a missing argument became undefined and slipped through quietly. So what does this line do with the 5?

02:03 Your turn: pause the video and predict.

02:11 It throws a TypeError.

02:14 Unlike the missing argument, this doesn’t go quiet. The stack trace points to line 8. quote() is a function, so it’s the message’s second cause: the return value isn’t iterable.

02:28 Iterable means what it means in Python, something you can loop over, and a number isn’t. Python stops the same way. The real slip is back up in quote()’s return, which ran without a word.

02:40 So when destructuring fails, read the function’s return first.

02:47 The fix goes where the results leave the function. Put the fee and the days in an array: return, then the pair in square brackets. Run it again. The first call logs the pair, 744 and 5.

03:03 And the destructuring unpacks it into the fee and the days, as Python’s unpacking does.

03:09 The fee prints as 744, without Python’s point zero. JavaScript numbers don’t split into integers and floats, so a whole value prints without one. The array does the tuple’s job here: a pair, read by position.

03:26 That’s where the analogy ends: unlike a tuple, nothing stops code from changing the array later.

03:33 That’s the JavaScript way to hand back two results: an array, when the caller unpacks the pair right away, as quote()’s caller does. When the results deserve names, where Python might reach for a dict or a named tuple, a function returns an object instead, and the caller reads each one by name.

03:52 The short way to write that object comes later in this course. No tuples in JavaScript: return an array and destructure it. Next up is Work Out a Fee and a Turnaround, an exercise.

04:07 Starting from a teammate’s port, you’ll write quote() and the turnaround helper in JavaScript, return the fee and the days together, and print a line each for Bauer Legal and Café Linde.

Become a Member to join the conversation.