What does toReversed() do?
The toReversed() method is a new, safe counterpart to the older reverse() method,
added in the ECMAScript 2023 (ES14) standard.
What it does
toReversed() returns a new array in which the elements are in reverse order,
while the original array is not changed.
Example:
javascript
const arr = [1, 2, 3];
const reversed = arr.toReversed();
console.log(reversed); // [3, 2, 1]
console.log(arr); // [1, 2, 3] - the original stayed the sameUnlike reverse(), which mutates the array,
toReversed() makes a clean (immutable) copy.
Comparison with reverse()
| Method | Changes the original array | Returns |
|---|---|---|
reverse() | Yes | The same array (mutated) |
toReversed() | No | A new array (reversed) |
Comparison example:
javascript
const a = [1, 2, 3];
const b = a.reverse();
console.log(a); // [3, 2, 1] - changed
console.log(b); // [3, 2, 1] - the same reference
const c = a.toReversed();
console.log(c); // [1, 2, 3] - a new array
console.log(a); // [3, 2, 1] - the original is untouchedWhen to use it
- When you need to safely reverse an array without damaging the original.
- Great for functional and reactive programming, where it's important not to mutate data (for example, in React or Redux).
Briefly:
| What it does | Creates a reversed copy of the array |
|---|---|
| Changes the original array | No |
| Returns | A new array |
| Added in | ECMAScript 2023 |
| Example | [1, 2, 3].toReversed() -> [3, 2, 1] |
Related non-mutating methods (ES2023+):
JavaScript added a whole family of safe counterparts.
| New method | Old mutating counterpart | What it does |
|---|---|---|
toReversed() | reverse() | Reverses the array |
toSorted() | sort() | Sorts without changing the original |
toSpliced() | splice() | Returns a modified copy |
with() | direct assignment by index | Returns a copy with a replaced element |
Summary:
toReversed()is a non-mutating way to reverse an array, introduced in newer versions of JavaScript (ES2023).Safe, clean, predictable. Does not change the original array.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.