reverse(): Flipping Array Order
You'll learn how to flip the order of all elements in an array, turning the first into last and last into first.
What It Does
The reverse() method reverses an array in place, meaning it modifies the original array rather than creating a new one. Think of it like flipping a deck of cards upside down—the card that was on top is now on the bottom, and vice versa.
How It Works
Simply call reverse() on any array. The method rearranges all elements so that what was at index 0 moves to the last position, what was at index 1 moves to second-to-last, and so on.
const colors = ['red', 'green', 'blue'];
colors.reverse();
console.log(colors); // ['blue', 'green', 'red']
The method returns the reversed array, but remember—it's the same array, now modified:
const numbers = [1, 2, 3, 4, 5];
const result = numbers.reverse();
console.log(numbers); // [5, 4, 3, 2, 1]
console.log(result); // [5, 4, 3, 2, 1]
console.log(result === numbers); // true (same array)
Reversing Different Array Types
It works with any array content:
const empty = [];
empty.reverse(); // [] (nothing to reverse)
const single = ['only'];
single.reverse(); // ['only'] (stays the same)
const mixed = [1, 'two', true, null];
mixed.reverse(); // [null, true, 'two', 1]
The Mutation Reminder
Since reverse() mutates the original array, any variables pointing to that array will see the change. This is the same mutating behavior you've seen with push(), pop(), splice(), and similar methods.
Key Takeaway: Use reverse() to flip an array's element order in place—the first becomes last, the last becomes first, and the original array is permanently modified.