Mutation vs Immutability

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

▶ Do the exercises

Exercise 1 — Two Families of Methods

Imagine you call a method on your `team` array and later realize your original `team` was accidentally changed! Let's try to understand why. In JavaScript, array methods belong to two different families:

- Methods that return a new array (like `.map()`, `.filter()`, `.slice()`) leave the original array completely untouched. - Methods that modify the original array (like `.push()`, `.splice()`, `.sort()`) directly change the array they are called on.

const team = ["Aquachu", "Zap"]; // .map() returns a new array and leaves team untouched const upperTeam = team.map((name) => name.toUpperCase()); console.log(upperTeam); console.log(team); // .push() directly modifies team team.push("Pyrochu"); console.log(team);

Exercise 2 — The Two Lists

Here is a summary table of the two families—keep an eye on which side a method belongs to:

Watch out for the two biggest traps: - `.sort()` and `.reverse()` look harmless, but they modify the original array in place. - `.slice()` vs `.splice()` have almost the same name, but `.slice()` returns a safe copy while `.splice()` modifies the original array!

Exercise 3 — Quiz: Did It Change?

What does this code print?

const nums = [3, 1, 2]; nums.sort((a, b) => a - b); console.log(nums);
Options
• 1,2,3
• 3,1,2
• undefined
• 2,1,3

Hint : This method belongs to the family that changes the array itself.

Exercise 4 — Quiz: Untouched?

What does this code print?

const nums = [1, 2, 3]; nums.filter((n) => n > 1); console.log(nums.length);
Options
• 3
• 2
• 0
• undefined

Hint : This method returns something new and ignores the original.

Exercise 5 — Sorting Without Damage

When you need a modifying method but want to keep the original, work on a copy. The spread operator makes one in a single symbol:

const scores = [30, 10, 20]; const sorted = [...scores].sort((a, b) => a - b); console.log(sorted); console.log(scores);

Same idea with `reverse` and `splice`.

Exercise 6 — Protect the Original

Sort a copy, leaving `scores` in its original order.

Hint : One symbol builds a separate array before the modifying method runs.

Exercise 7 — Reverse Without Breaking

Create `flipped` as a reversed copy of `team`, leaving `team` in its original order.

Hint : Use the spread operator to copy `team` into a new array before calling `.reverse()`.

Exercise 8 — Which Family?

Match each method with its family.

Items to match
• push
• sort
• map
• slice
Definitions
• Modifies the array
• Modifies the array
• Returns a new array
• Returns a new array

Exercise 9 — Quiz: The Safe Version

You want a sorted list without changing `scores`. Which line is right?

Options
• const s = [...scores].sort();
• const s = scores.sort();
• const s = scores.sorted();
• scores.sort();

Hint : The modifying method must run on something other than the original.

Exercise 10 — Safe Leaderboard

Produce three different views of the same list of scores. The original must be identical at the end.

Step 1: print `"Original: X"` with the scores joined by a comma and a space. Step 2: build `best`, a copy sorted from highest to lowest, and print `"Best: X"` the same way. Step 3: build `topThree`, a copy of the first three of `best`, and print `"Top 3: X"`. Step 4: build `boosted`, every score plus 5, and print `"Boosted: X"`. Step 5: print `"Original again: X"` to prove nothing changed.

Expected output:

Original: 30, 95, 45, 60 Best: 95, 60, 45, 30 Top 3: 95, 60, 45 Boosted: 35, 100, 50, 65 Original again: 30, 95, 45, 60
Starter code
const scores = [30, 95, 45, 60]; // Write your code here
Back to the course