Open In App

Reverse an array in JavaScript

Improve
Improve
Like Article
Like
Save
Share
Report

To reverse an array in JavaScript, you can either use the built-in reverse() method or can use different multiple approaches given in the article.

Lets first create a simple array in JavaScript and then apply the possible approaches in examples.

Javascript




let numbers_array = [1, 2, 3, 4, 5];


Examples to reverse an Array in JavaScript

1. Using reverse() method to reverse an array

This approach is the simplest as well as the native approach which marks the usage of the reverse() method available under arrays in JavaScript. First, we will declare an array with certain values and then we will apply the reverse() method to it to print the reversed array.

Example : This example describes the above-explained approach.

Javascript




let numbers_array = [1, 2, 3, 4, 5];
 
console.log("Original Array: ");
console.log(numbers_array);
 
numbers_array.reverse();
 
console.log("Reversed Array: ");
console.log(numbers_array);


Output

Original Array: 
[ 1, 2, 3, 4, 5 ]
Reversed Array: 
[ 5, 4, 3, 2, 1 ]

2. Using for() loop to reverse an array

In this approach, we will use the for() loop to reverse an array. First, we will create an array and then use the for loop to print the array elements in reverse order.

Example: This example describes the above-explained approach.

Javascript




let original_array = [1, 2, 3, 4];
 
let reversed_array = [];
 
console.log("Original Array: ");
console.log(original_array);
 
for (let i = original_array.length - 1;
    i >= 0; i--) {
    reversed_array.push(original_array[i]);
}
 
console.log("Reversed Array: ");
console.log(reversed_array);


Output

Original Array: 
[ 1, 2, 3, 4 ]
Reversed Array: 
[ 4, 3, 2, 1 ]

3. Using unshift() method to reverse an array

This approach uses the JavaScript unshift() method. This method adds elements at the beginning of the array itself. We will use the forEach() loop that will perform operations on each element of the array. We will use a newly created array in which we will add the elements from the previous array but in reversed manner itself.

Example: This example describes the above-explained approach.

Javascript




let original_array = [1, 2, 3, 4, 5, 6];
 
let reversed_array = [];
 
console.log("Original Array: ");
console.log(original_array);
 
original_array.forEach((element) => {
    reversed_array.unshift(element);
});
 
console.log("Reversed Array: ");
console.log(reversed_array);


Output

Original Array: 
[ 1, 2, 3, 4, 5, 6 ]
Reversed Array: 
[ 6, 5, 4, 3, 2, 1 ]

4. Using reduce() method to reverse an array

In this approach, we reduce the function which applies the callback function on each element and gets a summarized result of all items in the accumulator. First, we will use reduce method on the array and take empty as an accumulator and append each element at the beginning of the array. In the end, we get the reverse of the original array. 

Example: This example uses reduce() method to reverse the given array.

Javascript




let original_array = [1, 2, 3, 4];
 
let reversed_array = [];
 
console.log("Original Array: ");
console.log(original_array);
 
reversed_array = original_array
    .reduce((acc, item) => [item].concat(acc), [])
 
console.log("Reversed Array: ");
console.log(reversed_array);


Output

Original Array: 
[ 1, 2, 3, 4 ]
Reversed Array: 
[ 4, 3, 2, 1 ]

5. Using map() function to reverse an array

To reverse the given array using map function first,

  • Initialize the test array. 
  • Use the map function on the test array which creates a new array with an element of the test array in reverse order. 
  • Print result. 

Example: This example describes the above-explained approach.

Javascript




let array = [1, 2, 3, 4, 5];
 
console.log("Original Array: ");
console.log(array);
 
reverse_array = array.map((item, idx) => array[array.length - 1 - idx])
 
console.log("Reversed Array: ");
console.log(reverse_array);


Output

Original Array: 
[ 1, 2, 3, 4, 5 ]
Reversed Array: 
[ 5, 4, 3, 2, 1 ]

6. Using Lodash _.reverse() method to reverse an array

Lodash _.reverse() is used to reverse the array. This function changes the original array and also returns a new array.

Example: This example uses Lodash _.reverse() method to reverse an array.

Javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
let array1 = [1, 2, 3, 4]
 
// Printing original Array
console.log("original Array1: ", array1)
 
// Using _.reverse() method
let reversedArray = _.reverse(array1);
 
// Printing the reversedArray
console.log("reversed Array: ", reversedArray)
 
// Printing original Array
console.log("original Array1: ", array1)


Output:

original Array1:  [ 1, 2, 3, 4 ]
reversed Array:  [ 4, 3, 2, 1 ]
original Array1:  [ 4, 3, 2, 1 ]

Explanation: This code snippet demonstrates the usage of lodash library’s `reverse()` method to reverse an array. The original array `array1` is printed, then reversed using `_.reverse()` method. Both the reversed and original arrays are logged to the console. Lodash simplifies array manipulation tasks.



Last Updated : 11 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads