JavaScript Array reverse() Method
Below is the example of Array reverse() method.
- Example:
<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>
- 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:
<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> |
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 and above
- Microsoft Edge 12 and above
- Mozilla Firefox 1.0 and above
- Safari 1 and above
- Opera 4 and above