Skip to content
Related Articles

Related Articles

Reverse an array in JavaScript

Improve Article
Save Article
  • Last Updated : 15 Sep, 2022
Improve Article
Save Article

In this article, we will try to understand how we can reverse an array using several methods in JavaScript. We will use the below syntax to declare an array. After declaring the array, we will use different approaches to reverse the array.

Syntax:

let array_name = [];

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

Example:

Javascript




<script>
    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);
</script>


Output:

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

Approach 2: Using reverse 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.

Example:

Javascript




<script>
    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);
</script>


Output:

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

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

Example:

Javascript




<script>
    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);
</script>


Output:

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

Approach 4: Using reduce() method: In this approach we we reduce function which apply callback function on each element and get summarized result of all item in accumulator. First, we will use reduce method on array and take empty as accumulator and append each element at the beginning of array. At the end we get the reverse of original array. 

Example:

Javascript




<script>
    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);
</script>


Output:

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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!