reverse()

ES3+

Reverses an array in place and returns the reference to the same array.

Syntax

array.reverse()

Return Value

Array

The reversed array

Examples

JavaScript
const numbers = [1, 2, 3, 4, 5];
numbers.reverse();
console.log(numbers);
Output:
// [5, 4, 3, 2, 1]

📌 When to Use

Use reverse() when you need to reverse the order of array elements in place. Common uses include reversing sorted data, displaying items in newest-first order, or implementing stack-like LIFO behavior.

⚠️ Common Mistakes

Forgetting that reverse() modifies the original array in place

Using reverse() in React/Vue state directly - this mutates state unexpectedly

Chaining reverse() incorrectly: arr.reverse().filter() modifies arr first

✅ Best Practices

For immutable reverse, use toReversed() (ES2023) or [...arr].reverse()

To reverse a string: str.split("").reverse().join("")

Copy first if you need the original: const reversed = [...arr].reverse()

⚡ Performance Notes

reverse() is O(n/2) as it swaps elements from both ends toward the middle. It's an in-place operation, so no additional memory is allocated. Creating a copy with spread first makes the overall operation O(n).

🌍 Real World Example

Displaying Messages in Chronological Order

Reverse an array of messages sorted oldest-first to display newest first

// Messages from API are sorted oldest first
const messages = await fetchMessages();  // [{id: 1, time: '10:00'}, ...]

// Display newest first without modifying original
const displayMessages = [...messages].reverse();

// For React state (always copy first)
setMessages(prev => [...prev].reverse());

// Reverse a string
const original = 'Hello';
const reversed = original.split('').reverse().join('');
// reversed: 'olleH'

Related Methods