Loading video player…

Trace Two Names to One Object

In the video, lastWeek and nextWeek looked like two meal plans, but they were two names for one array, so dropping Friday from one dropped it from both.

Key Points

  • A name points at an array, like an arrow. const nextWeek = lastWeek; doesn’t build anything: it gives the same array a second arrow.
  • Change that one array through either name, as nextWeek.splice(4, 1) does, and both names see it. Changing an array in place is called mutation.
  • .reverse() hands back the same array too: after const flipped = meals.reverse();, flipped and meals both show the flip.
  • const fixes only the arrow. It stops reassignment (lastWeek = [] throws TypeError: Assignment to constant variable.), but it doesn’t stop mutation of the array.
  • To give a name its own array, make a new one. lastWeek.slice() with no arguments copies the whole array into a new one.
  • The rule: an equals sign copies the arrow, not the array.

Final Code

Here is plan.js as the lesson leaves it. With .slice(), nextWeek gets its own array, so dropping Friday leaves lastWeek alone:

Language: JavaScript
const lastWeek = [
  { day: "Monday", cook: "Ngozi", meals: ["leftover rice", "jollof rice"] },
  { day: "Tuesday", cook: "Chidi", meals: ["egg sandwiches", "pepper soup"] },
  { day: "Wednesday", cook: "Ada", meals: ["pepper soup", "spaghetti and meatballs"] },
  { day: "Thursday", cook: "Emeka", meals: ["salad wraps", "bean tacos"] },
  { day: "Friday", cook: "Chidi", meals: ["bean tacos", "takeout pizza"] },
  { day: "Saturday", cook: "Ada", meals: ["pancakes", "veggie stir-fry"] },
  { day: "Sunday", cook: "Grandma Ifeoma", meals: ["moi moi", "chicken curry"] },
];

const nextWeek = lastWeek.slice();
nextWeek.splice(4, 1);

console.log("Next week:", nextWeek.length, "days");
console.log("Last week:", lastWeek.length, "days");

Running node plan.js prints:

Language: Shell
Next week: 6 days
Last week: 7 days

Each slot of that copy still holds a day record. In the next lesson, Make Shallow Copies, you’ll find out what the copy shares with lastWeek.

00:00 In the last lesson, you edited the family’s lists of dinners and groceries. Each time, you asked one question: does this change the original? Now the Okafors want to plan a whole week at once.

00:13 The script plan.js holds last week’s plan. It’s an array of seven day records, Monday to Sunday. Each record holds the day, the cook, and that day’s lunch and dinner.

00:25 Next week should start as a copy of last week. A natural first try is to copy it with an equals sign, so nextWeek equals lastWeek.

00:34 Then nextWeek drops Friday’s takeout night. .splice() removes one record at position four, and Friday sits there. The last two lines log how many days each week has.

00:47 Your turn: pause the video and predict how many days last week has now.

00:57 Last week has six days too. Run the file, and the terminal shows six days for next week, as planned, and six days for last week.

01:06 Friday’s takeout night is gone from last week. Yet the line that removes Friday names only nextWeek.

01:15 To see why, follow the script line by line, and draw what each line does. The code at the top builds an array of seven day records. The name lastWeek points at that array, like an arrow.

01:28 The line after it doesn’t build anything. The equals sign hands nextWeek the same array that lastWeek points at. So nextWeek gets a second arrow to one array.

01:39 You’ve met a method that does this. .reverse() flips Monday’s meals and hands back that same array. Catch it in a second name, flipped, and both names show the flip.

01:49 An equals sign does the same, with no method at all.

01:54 Now .splice() follows nextWeek’s arrow and removes Friday from the array it finds. That’s the only array there is. So when the last line follows lastWeek’s arrow, it finds six days.

02:07 Changing an array in place like this is called mutation. Every name that points at the array sees it.

02:14 The const in front of lastWeek didn’t protect the array, either. const fixes only the arrow. Pointing a name at a different array is called reassignment, and on a const name like lastWeek, it throws an error.

02:28 So nextWeek needs an array of its own. .slice() is a method you already know, and it builds a new array. With no start and no end, .slice() copies from the first item to the last, so you get the whole array.

02:42 Change the line that makes nextWeek, so it calls .slice() on lastWeek. Now that line builds a new array, with its own seven slots, and nextWeek’s arrow points at it.

02:54 .splice() follows nextWeek’s arrow and removes Friday from the copy only. Run the file again. Next week has six days, and last week still has seven.

03:06 So the rule is that an equals sign copies the arrow, not the array. To give a name its own array, make a new one, as .slice() does.

03:16 You’ve drawn names as arrows, watched a mutation show through two names, and given nextWeek its own array. But each slot in that copy holds a day record.

03:26 In the next lesson, Make Shallow Copies, you’ll find out what the copy shares with lastWeek.

Become a Member to join the conversation.