Spread Operator for Arrays

The full text of this chapter, to read or revisit. To practise, do the exercises.

β–Ά Do the exercises

Exercise 1 β€” Spreading an Array

Imagine you want to create a new team array containing your current team plus a new recruit. If you simply place the existing array inside the new one, you end up with an array inside another array:

const team = ["Aquachu", "Zap"]; const newTeam = [team, "Pyrochu"]; console.log(newTeam.length); console.log(newTeam); // [ [ 'Aquachu', 'Zap' ], 'Pyrochu' ]

Notice that `newTeam` only has 2 elements, because `team` stayed grouped as a single item! To unpack its elements individually into the new array, use the spread operator (`...`) right before the array name:

const team = ["Aquachu", "Zap"]; const newTeam = [...team, "Pyrochu"]; console.log(newTeam.length); console.log(newTeam); // [ "Aquachu", "Zap", "Pyrochu" ]

Exercise 2 β€” Choosing Where Elements Go

You can place `...team` anywhere inside the new array β€” at the start, in the middle, or at the end β€” depending on where you want the new elements to appear:

const team = ["Aquachu", "Zap"]; console.log([...team, "Pyrochu"]); console.log(["Pyrochu", ...team]);

You can even use `...` on multiple arrays in the same line to merge them into one:

const water = ["Aquachu"]; const fire = ["Pyrochu"]; const all = [...water, ...fire, "Zap"]; console.log(all);

Exercise 3 β€” Quiz: How Many Elements?

What does this code print?

const a = [1, 2]; const b = [3, 4]; console.log([...a, ...b].length);
Options
β€’ 4
β€’ 2
β€’ 3
β€’ 1

Hint : Each spread pours its elements out individually.

Exercise 4 β€” Add a Recruit

Build `newTeam` with every member of `team`, then Metachu at the end.

Hint : One symbol pours the array out, and a name in text form needs its quotes.

Exercise 5 β€” Copying an Array

Spread with nothing else makes a copy. The two arrays then live their own lives:

const team = ["Aquachu", "Zap"]; const backup = [...team]; backup.push("Metachu"); console.log(team.length); console.log(backup.length);

Pushing into `backup` left `team` untouched. That's the point: without the spread, both names would point at the same array, and you'd change both at once. The next chapters come back to this.

Exercise 6 β€” Quiz: Copy or Not

What does this code print?

const a = [1, 2]; const b = [...a]; b.push(3); console.log(a.length);
Options
β€’ 2
β€’ 3
β€’ 1
β€’ 0

Hint : Ask yourself whether b is the same array as a, or a separate one.

Exercise 7 β€” Spread in a Function Call

Spread also works in a function call: it turns an array into a list of arguments. `Math.max` takes numbers one by one, not an array, so this is exactly what it needs:

const levels = [12, 4, 15, 8]; console.log(Math.max(...levels));

Without the spread, `Math.max` would receive one array instead of four numbers, and wouldn't know what to do with it:

const levels = [12, 4, 15, 8]; console.log(Math.max(levels));

Exercise 8 β€” Highest Level

Print the highest level of the array.

Hint : One function compares numbers, and one symbol hands them over one by one.

Exercise 9 β€” Spread Cheat Sheet

`const a = [1, 2];`. Match each line with its result.

Items to match
β€’ [...a]
β€’ [...a, 3]
β€’ [0, ...a]
β€’ [a, 3]
Definitions
β€’ A copy of a
β€’ [1, 2, 3]
β€’ [0, 1, 2]
β€’ An array containing an array

Exercise 10 β€” Team Merge

Two trainers join forces for a tournament. Build the combined roster without modifying either original array.

Step 1: build `roster`, containing every member of `teamA`, then every member of `teamB`. Step 2: build `withCaptain`, which is `roster` with `"Metachu"` added at the front. Step 3: print `"Roster: N"` and `"With captain: N"` with their lengths, then `"Captain: X"` with the first element of `withCaptain`. Step 4: print `"Team A still has N"` to prove the original wasn't touched.

Expected output:

Roster: 4 With captain: 5 Captain: Metachu Team A still has 2
Starter code
const teamA = ["Aquachu", "Zap"]; const teamB = ["Pyrochu", "Florachu"]; // Write your code here
Back to the course