Array.reverse()
Reverses the array order in place (mutates) and returns the same array.
array.reverse()This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Reverses the array order in place (mutates) and returns the same array.
The reverse() method transposes the elements of the calling array in place, mutating the original array and returning a reference to it. This operation has a linear time complexity of O(n) as it performs approximately n/2 swaps. Because it mutates the reference, developers often mistakenly affect other parts of the application where the same array is shared. In sparse arrays, empty slots (holes) are preserved and their indices updated accordingly. For non-mutating needs in modern environments, Array.prototype.toReversed() is the modern alternative; however, for broad compatibility, creating a shallow copy via the spread operator or slice() before calling reverse() remains the standard pattern. The method is generic and can be applied to array-like objects using Function.prototype.call if they possess a length property and integer-keyed properties.
Quick reference
Syntax
array.reverse()
See it in practice
Examples
Basic In-Place Reversal
const items = ['apple', 'banana', 'cherry'];
const result = items.reverse();
console.log(items);
console.log(result === items);['cherry', 'banana', 'apple'] true
The reverse() method changes the 'items' array directly. It also returns a reference to that same array.
Immutable Reversal using Spread
const original = [1, 2, 3, 4];
const reversed = [...original].reverse();
console.log('Original:', original);
console.log('Reversed:', reversed);Original: [1, 2, 3, 4] Reversed: [4, 3, 2, 1]
By using the spread operator [...], we create a shallow copy first. Calling reverse() on the copy prevents the original array from being mutated.
Sorting in Descending Order
const scores = [10, 50, 20, 90];
// sort() mutates and returns the reference, then reverse() mutates it again
scores.sort((a, b) => a - b).reverse();
console.log(scores);[90, 50, 20, 10]
While sort() can be configured for descending order via its callback, chaining reverse() after a default ascending sort is a common pattern for readability.
Debug faster
Common Errors
LogicError
Cause: Unexpected side effects when reversing an array that is shared across different parts of an application (e.g., props in a UI component).
Fix: Clone the array using the spread operator or slice() before reversing if the original data must be preserved.
function getTopItems(arr) {
return arr.reverse().slice(0, 2); // Error: Mutates the input 'arr'
}LogicError
Cause: Assuming the return value is a new array, leading to unexpected behavior when comparing the original and the result.
Fix: Remember that reverse() returns the same reference. Use a copy if you need two distinct versions of the data.
const a = [1, 2];
const b = a.reverse();
b.push(3);
console.log(a); // [2, 1, 3] - a was modified by b's pushRuntime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Reverses the array order in place (mutates) and returns the same array.
LogicError: Clone the array using the spread operator or slice() before reversing if the original data must be preserved. LogicError: Remember that reverse() returns the same reference. Use a copy if you need two distinct versions of the data.