Reverse an Array in JavaScript
Reverse an array in JavaScript with reverse(), a spread copy, loops and two pointers. Learn why reverse() changes the original. Explanation and output.
1// Method 1: Using reverse() method (mutates original)2let arr1 = [1, 2, 3, 4, 5];3arr1.reverse();4console.log("Using reverse():", arr1);56// Method 2: Using reverse() without mutation (spread operator)7let arr2 = [1, 2, 3, 4, 5];8let reversed = [...arr2].reverse();9console.log("\nOriginal:", arr2);10console.log("Reversed (new array):", reversed);1112// Method 3: Using for loop13function reverseArray(arr) {14 let reversed = [];15 for (let i = arr.length - 1; i >= 0; i--) {16 reversed.push(arr[i]);17 }18 return reversed;19}2021console.log("\nUsing loop:", reverseArray([10, 20, 30, 40]));2223// Method 4: Using while loop with two pointers24function reverseArrayTwoPointers(arr) {25 let left = 0;26 let right = arr.length - 1;27 let reversed = [...arr];2829 while (left < right) {30 // Swap elements31 [reversed[left], reversed[right]] = [reversed[right], reversed[left]];32 left++;33 right--;34 }35 return reversed;36}3738console.log("\nTwo pointers:", reverseArrayTwoPointers([1, 2, 3, 4, 5]));3940// Method 5: Using reduce41function reverseArrayReduce(arr) {42 return arr.reduce((acc, item) => [item, ...acc], []);43}4445console.log("\nUsing reduce:", reverseArrayReduce(['a', 'b', 'c', 'd']));4647// Method 6: In-place reversal (mutates original)48function reverseInPlace(arr) {49 let left = 0;50 let right = arr.length - 1;5152 while (left < right) {53 [arr[left], arr[right]] = [arr[right], arr[left]];54 left++;55 right--;56 }57 return arr;58}5960let original = [1, 2, 3, 4];61console.log("\nIn-place:", reverseInPlace(original));62console.log("Original mutated:", original);
Sample Output
Using reverse(): [ 5, 4, 3, 2, 1 ] Original: [ 1, 2, 3, 4, 5 ] Reversed (new array): [ 5, 4, 3, 2, 1 ] Using loop: [ 40, 30, 20, 10 ] Two pointers: [ 5, 4, 3, 2, 1 ] Using reduce: [ 'd', 'c', 'b', 'a' ] In-place: [ 4, 3, 2, 1 ] Original mutated: [ 4, 3, 2, 1 ]
In this one
- Which array methods mutate the original
- How to reverse a copy safely
- How the two-pointer swap works
- How to reverse with a backward loop
How it works
- 1
arr1.reverse() flips arr1 itself.
- 2
[...arr2].reverse() leaves arr2 unchanged.
- 3
The backward loop builds [40, 30, 20, 10].
- 4
Two pointers swap ends on a copy.
- 5
reduce builds [d, c, b, a].
- 6
reverseInPlace changes original to [4, 3, 2, 1].
Edge cases
Reversing a shared array
Calling reverse() on data used elsewhere changes it everywhere.
Copy first with [...arr] or use toReversed().
Empty or one-item arrays
Every method returns [] or the same single item.
The two-pointer loop does not run because left is not less than right.
What's going on
This program flips the order of an array. [1, 2, 3, 4, 5] becomes [5, 4, 3, 2, 1]. You might reverse a list of recent orders so the newest shows first.
The biggest lesson here is mutation. To mutate means to change the original. arr.reverse() mutates. It flips the array in place and also returns it. If other code still uses that array, it now sees the reversed order too. Then you spend time debugging why the original list is backwards.
Method 2 avoids that. [...arr2].reverse() first copies the array with spread, then reverses the copy. The output shows the original stays [1, 2, 3, 4, 5]. Newer JavaScript also has arr.toReversed(), which returns a reversed copy in one step.
Method 3 builds a new array with a loop that starts at the last index and counts down, pushing each item.
Method 4 uses two pointers on a copy. left starts at 0 and right at the end. They swap values with destructuring, [a, b] = [b, a], then move toward each other until they meet.
Method 5 uses reduce to put each item in front of the result. It is short but slow on big arrays, because it creates a new array each step.
Method 6 does the two-pointer swap on the real array. The output proves the original variable changed.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
After let a = [1, 2]; a.reverse(); what is a?
Pick an answer to check it.
Question 2
Which line keeps the original array unchanged?
Pick an answer to check it.
Questions people ask
Does reverse() return a new array?
No. It returns the same array, now reversed.
Is toReversed() safe to use?
It works in modern browsers and Node 20+. For older targets, use [...arr].reverse().
Can I reverse a string the same way?
Split it into an array first, reverse, then join.