Loading video player…

Returning Two Results at Once

Returning Two Results at Once

Python’s quote() hands back the fee and the turnaround with return fee, days: for the 6,200-word contract without the rush, the tuple (744.0, 5), which the caller unpacks with fee, days = quote(6200).

  • The comma loses the fee. Ported line by line, return fee, days; makes console.log(quote(6200)) log only 5, with no error. JavaScript has no tuples: its comma is an operator that evaluates both sides and keeps the last value. Parentheses don’t help: return (fee, days); still gives 5.
  • The caller fails instead. const [fee, days] = quote(6200); throws TypeError: quote is not a function or its return value is not iterable, at line 8. A number isn’t iterable, and Python’s fee, days = 5 stops the same way. The real slip is in the return, which ran without a word, so when destructuring fails, read the function’s return first.
  • The fix is an array. It logs [ 744, 5 ], with no .0: JavaScript numbers don’t split into integers and floats. It does the tuple’s job here, a pair read by position, though nothing stops code from changing it.
Language: JavaScript
function quote(words) {
  const fee = Math.max(words * 0.12, 40);
  const days = 2 + Math.floor(words / 2000);
  return [fee, days];
}

console.log(quote(6200));
const [fee, days] = quote(6200);
console.log(fee, days);

It logs the array, then the fee and the days:

Language: Program Output
[ 744, 5 ]
744 5

The JavaScript way: an array when the caller unpacks a pair right away, an object when the results deserve names, which the caller reads by name, not by position (the short way to write it comes later in this course).

Next up: Work Out a Fee and a Turnaround, an exercise.

00:00 Python’s quote works out the fee and the turnaround and hands both back at once. For the 6,200-word contract, leaving the rush aside for now, that’s a tuple: 744.0 and 5.

00:13 The caller unpacks it. The line-by-line port keeps that return line, comma and all. In this lesson, you’ll see what the comma does, and get both results back.

00:28 Run the port, which logs whatever quote returns: only 5. The fee is gone, and nothing complains. JavaScript has no tuples. Python’s comma builds one, but JavaScript’s comma is an operator.

00:45 It evaluates the fee and drops it, then keeps the days. Parentheses don’t help: return (fee, days) still gives 5.

00:57 Now port the unpacking as array destructuring, as in a for...of header. A missing argument went quietly undefined. So what does this line do with the 5?

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

01:17 It throws a type error: no quiet undefined here.

01:22 The trace points to line 8. The message gives two causes, and quote is a function, so it’s the second: the return value isn’t iterable. A number can’t be looped over, and Python stops the same way.

01:39 The real slip is in quote’s return, which ran silently, so when destructuring fails, read the return first. The fix goes there too: an array, the pair in square brackets.

01:53 Run it again: the first call logs the pair, 744 and 5, with no point zero: JavaScript numbers don’t split into integers and floats. Destructuring unpacks it.

02:07 The array does the tuple’s job, a pair read by position, but unlike a tuple, nothing stops code from changing it.

02:16 So return an array for a pair the caller unpacks at once. For results that deserve names, where Python might reach for a dict or a named tuple, return an object, and the caller reads each one by name, not by position.

02:31 The short way to write one comes later in this course. No tuples in JavaScript: return an array and destructure it. Next up is the exercise Work Out a Fee and a Turnaround, where you’ll port quote and the turnaround helper, then print a line each for Bauer Legal and Café Linde.

Become a Member to join the conversation.