Sub topic 8
toReversed: the related copying method
This chapter assumes you know how JavaScript arrays and indexing work, that you have used Array.prototype.reverse, and that you understand the difference between a primitive value and an object reference. ES2023 added four copying methods together. You have already seen toSorted, toSpliced, and with. The fourth is toReversed, and it is the simplest of them.
The mutating original: reverse
reverse() reverses the array in place. It returns the same array it was called on, not a new one. Because the return value is the original reference, code like this looks safe but is not:
const steps = ["mix", "bake", "cool"];
const backwards = steps.reverse();
console.log(backwards); // ["cool", "bake", "mix"]
console.log(steps); // ["cool", "bake", "mix"] (changed)
console.log(backwards === steps); // trueAssigning the result to a new name such as backwards does not make a copy. Both names point to one array, and that array has been reordered.
The copying version: toReversed
toReversed() takes no arguments. It returns a new array with the elements in reverse order and leaves the original untouched.
const steps = ["mix", "bake", "cool"];
const backwards = steps.toReversed();
console.log(backwards); // ["cool", "bake", "mix"]
console.log(steps); // ["mix", "bake", "cool"] (unchanged)
console.log(backwards === steps); // falseA common error: reversing on every call
The mutation in reverse causes a well-known bug. A function that is called more than once, such as a render function, flips the shared array every time it runs:
const messages = ["first", "second", "third"];
function latestFirst() {
return messages.reverse();
}
console.log(latestFirst()[0]); // "third"
console.log(latestFirst()[0]); // "first" (flipped back)If you replace messages.reverse() with messages.toReversed(), the function returns "third" on every call, because messages never changes.
Behavior that matches the other copying methods
toReversed follows the same rules as toSorted, toSpliced, and with:
| Behavior | `reverse()` | `toReversed()` |
|---|---|---|
| Changes the original | Yes | No |
| Return value | Same array | New array |
| Holes in sparse arrays | Kept as holes | Become `undefined` |
| Object elements | Same references | Same references (shallow copy) |
| Available on typed arrays | Yes | Yes |
The copy is shallow. If the array holds objects, the new array points to the same objects, and a later chapter covers what that means for you. Holes are read as undefined, so the result is always dense:
const sparse = [1, , 3];
console.log(sparse.toReversed()); // [3, undefined, 1]
console.log(1 in sparse.toReversed()); // true
const nums = new Int16Array([1, 2, 3]);
console.log(nums.toReversed()); // Int16Array [3, 2, 1]In runtimes that do not support ES2023, [...arr].reverse() gives the same result. It also turns holes into undefined, because spreading reads every index.
Exercise
Given const a = [1, 2, 3]; const b = a.reverse(); const c = a.toReversed();, what are the values of a, b, and c, and is b === a?
Show answer
reverse() mutates a to [3, 2, 1] and returns that same array, so b is [3, 2, 1] and b === a is true. toReversed() then reverses the already-reversed a into a new array, so c is [1, 2, 3]. After the last call, a is still [3, 2, 1].
Exercise
Rewrite function oldestFirst(list) { return list.reverse(); } so that calling it never changes the caller's array, and give a fallback for runtimes without ES2023.
Show answer
Use return list.toReversed();. The fallback is return [...list].reverse();, which reverses a fresh copy, so the caller's array stays unchanged.