How to use map function In Javascript

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 ]

Reverse an array in JavaScript

Reversing an array is a common task in JavaScript, useful in various scenarios such as data manipulation, algorithm development, and UI rendering. This guide covers multiple methods to reverse an array effectively.

Why Reverse an Array?

Reversing an array can be essential for data processing, enhancing user experience, or solving specific algorithmic problems. Whether it’s for displaying data in reverse order or transforming datasets, understanding how to reverse arrays efficiently is crucial for developers.

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

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

Similar Reads

Examples to reverse an Array in JavaScript

Table of Content 1. Using reverse() method2. Using for loop3. Using unshift method4. Using reduce method5. Using map() function to reverse an array6. Using Lodash _.reverse() method to reverse an array7.Using recursion...

1. Using reverse method

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....

2. Using for loop

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....

3. Using unshift method

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....

4. Using reduce method

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....

5. Using map function

To reverse the given array using map function first,...

6. Using Lodash _.reverse method

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

7. Using recursion

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....