Mutating vs Copying
Two camps, and nothing in the syntax to tell them apart
Every list method belongs to one of two camps, and mixing them up is the most common list bug there is.• A mutating method reaches into the list you already have and rearranges it. Your list is different afterwards.
• A copying method leaves your list exactly as it was and hands back something new: a new list, a string, a number, or a true or false.
Both camps are written the same way, with a dot and a name and a pair of parentheses. You cannot tell which is which by looking. You have to know.
| Method | The list you called it on | What it hands back |
|---|---|---|
| push(v) | MUTATES: grows by one at the end | the new length |
| pop() | MUTATES: loses its last value | the removed value |
| unshift(v) | MUTATES: grows by one at the front | the new length |
| shift() | MUTATES: loses its first value | the removed value |
| splice(i, n) | MUTATES: loses n values from position i | a list of what was removed |
| sort() | MUTATES: reordered where it stands | that same list again |
| reverse() | MUTATES: turned around where it stands | that same list again |
| slice(a, b) | untouched | a new list |
| concat(other) | untouched | a new list |
| join(sep) | untouched | a string |
| indexOf(v) | untouched | a position, or -1 |
| includes(v) | untouched | true or false |
The default sort is not a numeric sort
This is the one that costs people whole afternoons, because the output looks sorted. It is sorted. It is just not sorted the way you meant.The program below sorts the same distances twice, first with no comparator and then with
(a, b) => a - b. Run it, then change it and run it again:• Add 1000 to the array.
• Change
(a, b) => a - b to (a, b) => b - a.• Change the array to
[85, 59, 96, 69], four numbers with two digits each.let distances = [140, 7, 56, 230];
distances.sort();
console.log("Default sort:", distances);
distances.sort((a, b) => a - b);
console.log("Number sort:", distances);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
reverse hands back the same list, not a copy
The mutating methods are misleading in a second way. Several of them return something, so an assignment looks perfectly reasonable, and it is what they return that catches people out.Two names can mean one list
An assignment between two list variables does not duplicate anything. What gets copied is the reference, the note saying where the list is kept. Both names end up holding the same note, so a change made through either one is visible through both.In the program below,
alsoVotes is a second name for votes, and saved is a copy made with slice. Run it, then make each change below and run it again. Press Reset before each change, so that each one starts from the original program.• Change
alsoVotes[0] = 98; to votes[0] = 98;.• Change
alsoVotes[0] = 98; to saved[0] = 98;.• Cut the line
let saved = votes.slice(); and paste it below alsoVotes[0] = 98;.let votes = [75, 56, 89];
let alsoVotes = votes;
let saved = votes.slice();
alsoVotes[0] = 98;
console.log("votes:", votes);
console.log("alsoVotes:", alsoVotes);
console.log("saved:", saved);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.