Sub topic 2
The copying methods added in ES2023
This chapter assumes you know JavaScript arrays and indexing, sort and splice, arrow functions, and the difference between primitive values and object references.
The last chapter showed how sort, splice, and bracket assignment change an array in place. ECMAScript 2023 added a set of methods, through the "Change Array by Copy" proposal, that do the same jobs without touching the original. This chapter covers what those methods are, how each one matches an older method, and the rules they all share.
The four methods and their mutating counterparts
Each new method is a copying version of an existing operation. It takes the same arguments, or very nearly the same, and it returns a new array.
| Mutating operation | Copying method | What the copying method returns |
|---|---|---|
| `arr.sort(compare)` | `arr.toSorted(compare)` | A new sorted array |
| `arr.reverse()` | `arr.toReversed()` | A new reversed array |
| `arr.splice(start, count, ...items)` | `arr.toSpliced(start, count, ...items)` | The new array after the change, not the removed elements |
| `arr[i] = value` | `arr.with(i, value)` | A new array with one index replaced |
Seeing them side by side
The quickest way to learn the methods is to run all four on the same input and then check the input.
const original = [3, 1, 2];
console.log(original.toReversed()); // [2, 1, 3]
console.log(original.toSorted()); // [1, 2, 3]
console.log(original.toSpliced(0, 1)); // [1, 2]
console.log(original.with(0, 9)); // [9, 1, 2]
console.log(original); // [3, 1, 2] (unchanged)Compare this with the return value of sort. It hands back the same array object it was called on, so code that looks like it creates a sorted copy actually holds two names for one array:
const nums = [3, 1, 2];
const sortedInPlace = nums.sort();
console.log(sortedInPlace === nums); // true: same array
const sortedCopy = nums.toSorted();
console.log(sortedCopy === nums); // false: a new arrayRules all four methods share
The later chapters cover each method in detail. These properties apply to all four:
- The receiver is never modified. The array you call the method on keeps its contents and its length.
- The result is always a new plain
Array. Calling one of these methods on a subclass ofArraystill gives you an ordinaryArray. - The copy is shallow. If the elements are objects, the new array holds references to the same objects as the original.
- The result is dense. Holes in a sparse array come out as
undefinedin the new array. - Typed arrays get most of them.
Uint8Arrayand the other typed arrays havetoSorted,toReversed, andwith, but they do not havetoSpliced.
Common mistakes
- Writing
arr.toSorted()and not using the result. The method has no side effect, so the call does nothing. - Expecting a deep copy. Changing
copy[0].namealso changes the object in the original. - Reaching for
toSplicedto fetch removed items. Useslicefor that.
Exercise
Given const a = [5, 4, 3];, what do a.toSpliced(1, 1) and a contain afterward, and how would the answer change if you used a.splice(1, 1) instead?
Show answer
a.toSpliced(1, 1) returns [5, 3], and a is still [5, 4, 3]. With a.splice(1, 1), the call returns [4], the removed elements, and a becomes [5, 3].
Exercise
A teammate writes const top = scores.toSorted((x, y) => y - x); top.push(0);. Is scores affected by either line?
Show answer
No. toSorted returns a new array, so push changes top only. scores keeps its original order and length.