Last modified: Jul 27, 2026
JavaScript Reverse Array Guide
Reversing an array is a common task in JavaScript. You might need to flip the order of items for display, processing, or logic. This guide covers the main ways to reverse an array.
We will explore the reverse() method. We will also look at manual loops. Finally, we will see how to reverse without mutating the original array.
Using the Reverse Method
The reverse() method is the simplest way. It reverses the array in place. This means it changes the original array. The first element becomes the last, and the last becomes the first.
Here is a basic example:
// Original array
let fruits = ['apple', 'banana', 'cherry'];
console.log('Original:', fruits);
// Reverse the array
fruits.reverse();
console.log('Reversed:', fruits);
The output will show the reversed order:
Original: [ 'apple', 'banana', 'cherry' ]
Reversed: [ 'cherry', 'banana', 'apple' ]
Notice the original array fruits is now changed. This is important to remember. If you need to keep the original array, use a different approach.
Reverse Without Mutating the Original
To reverse an array without changing the original, use the spread operator or slice(). First, create a copy of the array. Then reverse the copy.
Here is how to do it with the spread operator:
let numbers = [1, 2, 3, 4, 5];
// Create a copy and reverse it
let reversedNumbers = [...numbers].reverse();
console.log('Original:', numbers);
console.log('Reversed copy:', reversedNumbers);
The original array stays the same:
Original: [ 1, 2, 3, 4, 5 ]
Reversed copy: [ 5, 4, 3, 2, 1 ]
You can also use slice() to make a copy. The slice() method returns a new array. Then call reverse() on that new array.
let colors = ['red', 'green', 'blue'];
let reversedColors = colors.slice().reverse();
console.log('Original:', colors);
console.log('Reversed:', reversedColors);
This method is safe and widely used. For more details on array methods, check out our JavaScript Array Methods Guide.
Reverse Using a Loop
You can also reverse an array manually using a for loop. This gives you more control. It is also a good way to understand how reversal works under the hood.
The idea is to swap elements from the start and end. Move towards the center.
function reverseArray(arr) {
let left = 0;
let right = arr.length - 1;
while (left < right) {
// Swap elements
let temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
return arr;
}
let letters = ['a', 'b', 'c', 'd'];
console.log('Original:', letters);
reverseArray(letters);
console.log('Reversed:', letters);
This loop reverses the array in place. Here is the output:
Original: [ 'a', 'b', 'c', 'd' ]
Reversed: [ 'd', 'c', 'b', 'a' ]
This method is efficient. It uses O(n/2) swaps, which is very fast. It is also a common interview question.
Reverse Using Reduce
The reduce() method can also reverse an array. It works by building a new array from the original. Each element is added to the front of the new array.
Here is an example:
let items = [10, 20, 30, 40];
let reversedItems = items.reduce((acc, current) => {
return [current, ...acc];
}, []);
console.log('Original:', items);
console.log('Reversed:', reversedItems);
The output shows the reversed array:
Original: [ 10, 20, 30, 40 ]
Reversed: [ 40, 30, 20, 10 ]
This method is functional and clean. However, it creates a new array each iteration. For large arrays, this might be slower than the loop method.
Reverse Array of Objects
Reversing an array of objects works the same way. The reverse() method works on any array. The objects themselves are not changed, only their order.
Here is an example:
let users = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 }
];
let reversedUsers = [...users].reverse();
console.log('Original:', users);
console.log('Reversed:', reversedUsers);
The output shows the reversed order of objects:
Original: [ { name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }, { name: 'Charlie', age: 35 } ]
Reversed: [ { name: 'Charlie', age: 35 }, { name: 'Bob', age: 30 }, { name: 'Alice', age: 25 } ]
This is useful for sorting or displaying data in reverse order. For more on working with arrays, see our JavaScript Array Variables Guide.
Performance Considerations
The reverse() method is very fast. It is built into the JavaScript engine. For most cases, it is the best choice.
Manual loops are also fast. They give you more control. Use them when you need to customize the reversal logic.
The spread operator and reduce() create new arrays. This uses more memory. For small arrays, this is fine. For large arrays, consider the in-place methods.
Always test with your data. Use the console.time() method to measure performance.
Common Mistakes
One common mistake is forgetting that reverse() mutates the array. Always make a copy if you need the original.
Another mistake is using reverse() on a string. Strings are not arrays. You must first split the string into an array.
Here is how to reverse a string:
let word = 'hello';
let reversedWord = word.split('').reverse().join('');
console.log(reversedWord); // Output: 'olleh'
This is a common pattern for string reversal.
Conclusion
Reversing an array in JavaScript is easy. Use the reverse() method for a simple in-place reversal. Use the spread operator or slice() to keep the original array unchanged. Use a manual loop for more control or performance tuning.
Remember to always consider whether you need to mutate the original array. Choose the method that best fits your needs. Practice with different arrays to build confidence.
For more advanced array techniques, explore our JavaScript Array Methods Guide. It covers many other useful methods.