Open In App

JavaScript Array reverse() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The JavaScript Array reverse() method reverses the order of the elements in an array in place. The first array element becomes the last, and the last element becomes the first, and so on.

It modifies the original array and returns a reference to the reversed array. We can also use the Array.toReversed() which returns the new array with the elements in the reverse order.

Syntax:

arr.reverse()

Parameters:

This method does not accept any parameter

Return value:

Returns the reference of the reversed original array.

JavaScript Array reverse() Method Examples

Example 1: Reversing Array Order with JavaScript Array reverse()

The function func() initializes an array arr with elements. It logs the original array, then reverses its order using the reverse() method, storing the result in new_arr, and logs the reversed array.

JavaScript
function func() {
    // Original Array
    let arr = ['Portal', 'Science',
        'Computer', 'GeeksforGeeks'];
    console.log(arr);

    // Reversed array
    let new_arr = arr.reverse();
    console.log(new_arr);
}
func();

Output
[ 'Portal', 'Science', 'Computer', 'GeeksforGeeks' ]
[ 'GeeksforGeeks', 'Computer', 'Science', 'Portal' ]

Example 2: Reversing Array Order with JavaScript Array reverse():

In this example, the reverse() method reverses the sequence of the array elements of arr.

JavaScript
function func() {

    // Original Array
    let arr = [34, 234, 567, 4];
    console.log(arr);

    // Reversed array
    let new_arr = arr.reverse();
    console.log(new_arr);
}
func();

Output
[ 34, 234, 567, 4 ]
[ 4, 567, 234, 34 ]

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browsers:

The browsers supported by the JavaScript Array reverse() method are listed below:

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


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