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

Follow Two Names to One Array

In this video, you saved tonight’s game queue for an undo and found that a second name doesn’t make a copy. Here are the key points:

  • A name is an arrow to its array or object. const saved = queue; copies the arrow, not the array, so both names point at one array.
  • A change through one name shows through every name. After queue.push("Carcassonne"), saved shows four games too.
  • Make a real copy when you need a separate one. queue.slice() with no arguments copies the whole array, and [...queue] makes the same copy.
  • const fixes where the arrow points, not what’s in the array. That’s why push() works on a const array, and why queue = saved; throws TypeError: Assignment to constant variable. Declare the name with let to point it somewhere else. Afterward, both names share one array again, so copy the queue into a new name before the next change.
  • A function’s parameter gets a copy of the arrow, so a function can change the caller’s array or record. Choose on purpose whether to change the record or to copy it with { ...player } and return the new one.
  • These copies are one level deep. A copy of players gets new slots, with arrows to the same records, so changing a record through the copy changes it for players too. withFriend is still safe, because it copies the record itself, and a record’s fields here are plain values.

Here’s the copy that keeps the saved queue separate, and the undo that points the queue back at it. At the end of main.js, replace the last lesson’s queue experiment with these lines, so queue is declared only once:

Language: JavaScript
let queue = ["Azul", "Catan", "Codenames"];
const saved = queue.slice();
queue.push("Carcassonne");
console.log("Saved:", saved);
console.log("Queue:", queue);
queue = saved;
console.log("Queue:", queue);

Save, and the Console shows the saved copy, the queue with Carcassonne, and the queue back at three games:

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 Last lesson, reverse() handed back the very array it changed, so reversed and restored showed one order. In this video, you’ll see why two names can lead to one array.

00:12 Priya wants to undo changes to the queue, so the code should keep the queue as it was. At the end of main.js, replace the last lesson’s queue experiment with a new queue: Azul, Catan and Codenames.

00:25 A natural first try is a second name, saved, set to the queue. Then Wei suggests Carcassonne, and push() adds it to the queue. A log line shows the saved queue.

00:37 Your turn: pause the video and predict what the log line shows.

00:46 Save, and the Console shows four games in saved, Carcassonne included. The saved queue changed along with the queue. So this first try is a detour, because saved never held a copy.

01:01 Behind the scenes, the first line makes one array, and the name queue is an arrow pointing at it. The second line copies the arrow, not the array. Now saved points at that same array.

01:14 The push() call adds Carcassonne to that one array, so both names show it.

01:20 To keep the queue as it was, make a second array ahead of the push. Calling slice() with no arguments copies the whole array, as you saw last lesson.

01:30 Now saved points at an array of its own. The push() call adds Carcassonne to the queue’s array only. Save, and saved still shows three games, while the queue shows four.

01:42 Three dots inside square brackets make the same copy, a new array holding the queue’s items.

01:49 Now the undo itself: point the queue back at the saved array, then log the queue. A const name can’t be pointed anywhere else, so with const, that line throws a TypeError.

02:01 The keyword const fixes where the arrow points and leaves what’s in the array alone. That’s why push() worked. Declare the queue with let, and the undo moves the queue’s arrow back to the saved array.

02:14 Save, and the last line shows three games again. Both names share one array once more. The saved copy has done its job. To save again, slice the queue into a new name before the next change.

02:29 Records follow the same rule. Wei is bringing one more friend. The top of main.js already holds the players array, and each slot points at a player’s record.

02:39 A function, addFriend, takes a player and adds one to its party. You call it with Wei’s record, players at index two. Your turn: pause the video and predict what the log line shows for Wei’s party.

02:58 Save, and Wei’s party is three.

03:02 The parameter player got a copy of the arrow in slot two, so the function changed the caller’s data.

03:09 Later in this course, you’ll want exactly that, when a click changes a player’s record and your code draws the page again from the records.

03:19 Before saying yes, Priya wants to see Wei’s bigger party, with the guest list left as it was. So return a new record instead. A new function, withFriend, does that.

03:30 With the addFriend call gone, Wei’s record starts at two again. Three dots inside curly braces copy a record into a new object. The function changes the copy and returns it, and a new name, bigger, holds the result.

03:45 Save, and the new record’s party is three, while Wei’s record still says two. That’s all you need about the three dots for now.

03:56 As a rule, giving an array or object another name, or passing it to a function, copies the arrow, never the value. Make a copy when you need a separate one, and choose on purpose whether a function changes what it’s given or returns something new.

04:12 Next, in Exercise: Line Up Tonight’s Games, you’ll keep tonight’s game queue up to date for Priya’s guests.

Become a Member to join the conversation.