Reverse an Array in JavaScript
Given an Array, the task is to reverse the array in JavaScript.
Input: arr[] = {1, 2, 3}
Output: {3, 2, 1}Input: arr[] = {“a”, “c”}
Output: {“c”, “a”}
Using reverse() (In-Place) – Best and Most Used
let a = [ 1, 2, 3, 4, 5 ];
a.reverse();
console.log(a);
Output
[ 5, 4, 3, 2, 1 ]
Using toReversed() – Best when a New Array is Needed
let a = [1, 2, 3, 4, 5];
let b = a.toReversed();
console.log(a); // [1, 2, 3, 4, 5]
console.log(b); // [5, 4, 3, 2, 1]
Output:
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
Please refer Reverse an Array for different algorithms and analysis.
Other Methods
Using for Loop : We use the for loop to reverse an array in JavaScript. First, we will create an empty array and then use the for loop to insert the original array elements in reversed order.
const array = [ 1, 2, 3, 4, 5 ];
let revArray = [];
for( let i = array.length - 1; i >= 0; i-- ) {
revArray.push(array[i]);
}
console.log("Reversed Array: ", revArray);
Output
Reversed Array: [ 5, 4, 3, 2, 1 ]
Using unshift() Method : We will create an empty array revArray to store the reversed array elements. First, use the forEach() loop to iterate over each element of the original array and use unshift() method to insert each element at the start of revArray, reversing the order of elements.
let array = [ 1, 2, 3, 4, 5 ];
let revArray = [];
array.forEach((element) => {
revArray.unshift(element);
});
console.log("Reversed Array: ", revArray);
Output
Reversed Array: [ 5, 4, 3, 2, 1 ]
Using reduce() Method : We will use the reduce() method on original array and take empty as an accumulator and append each element at the beginning of the array. At the end, we will get the reverse array from the original array.
let array = [ 1, 2, 3, 4, 5 ];
let revArray = [];
revArray = array.reduce((acc, item) => [item].concat(acc), [])
console.log("Reversed Array: ", revArray);
Output
Reversed Array: [ 5, 4, 3, 2, 1 ]
Using map() Method : The map() method iterates over each element of the original array where index parameter represents the current position of the element. The array[array.length – 1 – index] accesses elements from the end of the array moving towards the beginning.
let array = [1, 2, 3, 4, 5];
let revArray = array.map(
(item, index) => array[array.length - 1 - index]);
console.log("Reversed Array: ", revArray);
Output
Reversed Array: [ 5, 4, 3, 2, 1 ]
Reverse an Array using Recursion in JavaScript : In JavaScript, reversing an array recursively involves popping elements from the original array and concatenating them with the result of recursively reversing the remaining elements until the array is empty, forming the reversed array.
function reverseArray(arr) {
if (arr.length === 0) {
return [];
} else {
return [arr.pop()].concat(reverseArray(arr));
}
}
const array = [ 1, 2, 3, 4, 5 ];
let revArray = reverseArray(array);
console.log("Reversed Array: ", revArray);
Output
Reversed Array: [ 5, 4, 3, 2, 1 ]
Using Lodash _.reverse() Method : Lodash _.reverse() method is used to reverse the array. This function changes the original array and also returns a new array.
Syntax
revArray = _.reverse(array);// Requiring the lodash library
const _ = require("lodash");
let array = [ 1, 2, 3, 4, 5 ]
let revArray = _.reverse(array);
console.log("Reversed Array: ", revArray);
Output
Reversed Array: [ 5, 4, 3, 2, 1 ]Become an expert in solving problems with DSA JavaScript—the course designed to teach you Data Structures and Algorithms using JavaScript. Over the next 90 days, dive deep into key DSA concepts, learn to optimize your code, and gain hands-on experience solving challenging problems. Whether you're a beginner or looking to refine your JavaScript skills, this course will provide the knowledge you need to succeed.
Take on the Three 90 Challenge today! Complete 90% of the course within 90 days, and you’ll earn a 90% refund. This challenge is your chance to stay motivated, improve your skills, and get rewarded for your progress. Don’t wait—start your journey to DSA mastery with JavaScript today!


