Course contentsShow
← JavaScript
Lesson 833 of 2,72319. ArraysFree lesson

toReversed() for Immutable Reversal

Return a reversed copy of an array without modifying the original.

toReversed() for Immutable Reversal

You'll learn how to create a reversed copy of an array while keeping the original array unchanged.


What Is toReversed()?

You've already learned about reverse(), which flips an array in place, modifying the original. The toReversed() method does the same reversal but returns a new array instead, leaving your original array untouched. It's the immutable alternative you reach for when you need a reversed version but don't want to alter the source.

Think of it like photocopying a document and then flipping the pages of the copy — the original document stays exactly as it was.


How It Works

const fruits = ['apple', 'banana', 'cherry'];
const reversed = fruits.toReversed();

console.log(reversed); // ['cherry', 'banana', 'apple']
console.log(fruits);   // ['apple', 'banana', 'cherry'] — unchanged!

No parameters needed. Just call toReversed() on any array, and you get back a brand-new array with elements in reverse order.


Why Use toReversed() Over reverse()?

When you're working with data you need to preserve (for logging, comparison, or because other code relies on it), toReversed() is safer. You avoid unexpected side effects from mutation.

const original = [1, 2, 3];
const option1 = original.reverse();    // mutates original
const option2 = original.toReversed(); // original stays safe

It pairs nicely with other immutable methods like slice(), concat(), and with() to build cleaner, more predictable code.


Key Takeaway:
Use toReversed() when you need a reversed array but want to preserve the original — it's the immutable, side-effect-free way to reverse.