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

Spread and Gather Arguments and Patterns

In the video, the same three dots worked in two directions. They spread the recipe box’s prep times out into a call to Math.max(), and they gathered Grandma Ifeoma’s extra dishes, and what was left of the recipe box, up into new arrays and objects.

Key Points

  • Math.max() takes its numbers as separate arguments. Math.max(...prepMinutes) takes each item out of the array, in order, and passes it as its own argument, so it’s the same call as Math.max(60, 45, 25, 50, 35) and returns 60.
  • Spread doesn’t change the array it spreads: prepMinutes keeps its five prep times, in order.
  • Without the dots, Math.max(prepMinutes) gets one argument, the whole array. Math.max() tries to turn that argument into a number, and five numbers can’t become one, so it returns NaN.
  • Spreading a string into a new array gives one letter per item: [..."egusi"] is [ 'e', 'g', 'u', 's', 'i' ].
  • In a parameter list, the dots gather. In function addMeals(record, ...meals), every argument after the day record lands in one new array, meals. That’s a rest parameter. With two dishes, meals holds two, with one it holds one, and with none it’s an empty array, []. In the body, [...record.meals, ...meals] builds a new array, where values go, so the same ...meals spreads the gathered dishes back out after the record’s own meals.
  • Patterns gather too. const [first, ...others] = recipes; puts the first recipe in first and the other four in a new array, others, while recipes keeps all five. const { day, ...rest } = monday; puts every property the pattern didn’t name, the cook and the meals, into a new object, rest. The object is new, but its meals are Monday’s own array, shared, not copied.
  • The rest always comes last, because it takes everything that’s left. const [...start, last] = recipes; won’t run: Node reports SyntaxError: Rest element must be last element. A rest parameter follows the same rule.
  • The rule: read the three dots by where they stand, not by the brackets around them. Where values go, in a call or in a new array or object you’re building, they spread out. Where names are made, in a parameter list or a pattern on the left of the =, they gather values up into a new array or object.

Final Code

prep.js spreads the prep times into Math.max():

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, you built new arrays and records with spread. In this video, you’ll see the same three dots work in two directions: spreading values out, and gathering them up.

00:12 Here’s prep.js, with the recipe box’s prep times, in minutes, in the prepMinutes array. You’ve found the longest one before by sorting a copy.

00:21 There’s a shorter way. Math.max() gives you the largest of the numbers you pass it. It takes them as separate arguments, one after another. Type the numbers in, run the file, and it prints sixty.

00:35 That works, but the numbers are already in the array. Every new recipe would mean editing this line.

00:43 Put three dots in front of the array, inside the call. JavaScript takes each item out of prepMinutes, in order, and passes it as its own argument.

00:53 So it’s the same call you typed, and it prints sixty again.

00:58 Spread doesn’t change the array it spreads. The last line logs it, and all five are still there, in order. Without the dots, Math.max() gets one argument, the whole array.

01:11 It tries to turn that into a number, and five numbers can’t become one. So it prints NaN, short for not a number.

01:20 Spread works on a string too. Spread egusi into a new array, and you get one letter per item.

01:29 Now the other direction. Grandma Ifeoma is cooking for guests next Sunday and wants to add two extra dishes, or maybe one, or none. Here’s sunday.js, with last Sunday’s record.

01:42 The helper, addMeals(), has a first parameter, record, for a day record. The second, ...meals, has three dots in front of it.

01:52 In a parameter list, the dots gather. Every argument after the day record lands in one new array, and meals points at it. That’s a rest parameter.

02:01 It collects the rest of the arguments.

02:05 The body logs what it gathered. Then it returns a new record. Inside its new array, ...meals stands where values go, so these dots spread the gathered dishes back out, after the record’s own meals.

02:19 Call it with last Sunday and two dishes, puff-puff and fried plantain. Both land in meals, in an array of two. Next Sunday gets four meals, and last Sunday keeps its two.

02:32 Call it with one dish, and the array holds one. Call it with none, and it’s an empty array.

02:40 Destructuring patterns can gather too. Here’s unpack.js, with the recipe box’s five recipes. The array pattern’s first name, first, takes the first recipe.

02:51 Then comes ...others, with three dots in front. Your turn: pause the video and predict what others holds.

03:04 It gathers whatever’s left, the other four recipes, into a new array. The recipe box itself still has all five.

03:13 An object pattern gathers by name. From Monday’s record, day takes the day’s name. Then ...rest gathers every property the pattern didn’t name, the cook and the meals, into a new object.

03:27 The object is new, but its meals are Monday’s own array, shared, not copied.

03:34 Watch out for the order. The rest always comes last, because it takes everything that’s left. Put a name after it, and the file won’t run. Node reports a syntax error.

03:45 A rest parameter follows the same rule.

03:49 So here’s the rule: read the three dots by where they stand, not by the brackets around them. Where values go, in a call or in a new array or object you’re building, they spread out.

04:01 Where names are made, in a parameter list or a pattern on the left of the equals sign, they gather values up into a new array or object.

04:11 You’ve spread the prep times into a call, gathered Grandma Ifeoma’s dishes, and gathered what’s left of the recipe box. In the next lesson, Use Copying Array Methods, you’ll use array methods that give back a changed copy and leave the original as it was.

Become a Member to join the conversation.