JavaScript Array reverse() Method

Below is the example of Array reverse() method.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
    function func() {
      
        // Original Array
        var arr = ['Portal', 'Science'
                   'Computer', 'GeeksforGeeks'];
        document.write(arr);
      
        // Reversed array
        var new_arr = arr.reverse();
        document.write("<br>");
        document.write(new_arr);
    }
    func();
    </script>                   

    chevron_right

    
    

  • Output:
    Portal,Science,Computer,GeeksforGeeks
    GeeksforGeeks,Computer,Science,Portal

The arr.reverse() method is used for in-place reversal of the array. The first element of the array becomes the last element and vice versa.

Syntax:

arr.reverse()

Parameters: This method does not accept any parameter

Return value: This method returns the reference of the reversed original array.



Below examples illustrate the JavaScript Array reverse() method:

  • Example 1: In this example the reverse() method reverses the sequence of the array elements of arr.
    var arr = [34, 234, 567, 4];
    print(arr);
    var new_arr = arr.reverse();
    print(new_arr);
    

    Output:

    34, 234, 567, 4
    4, 567, 234, 34
    

Code for the above method is provided below:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
function func() {
  
    // Original Array
    var arr = [34, 234, 567, 4];
    document.write(arr);
  
    // Reversed array
    var new_arr = arr.reverse();
    document.write("<br>");
    document.write(new_arr);
}
func();
</script>

chevron_right


Output:

34, 234, 567, 4
4, 567, 234, 34

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

  • Google Chrome 1.0
  • Microsoft Edge 5.5
  • Mozilla Firefox 1.0
  • Safari
  • Opera

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.