Tutorialspoint.dev

JavaScript | Array reverse()

arr.reverse() 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()

Argument
This function does not take any argument.

Return value
This function returns the reference of the reversed original array.

Example for the above function is provided below:

Example 1:



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

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

Code for the above function is provided below:

Program 1:

<script>
// JavaScript to illustrate reverse() function
  
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>

Output:

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


This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter