Saturday, September 21, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Array reverse() Method

JavaScript Array reverse() Method

JavaScript Array.reverse() Method is used for the in-place reversal of the array. This method reverses the order of the array. The first element of the array becomes the last element and the last one becomes the first. It mutates the original 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: This method returns the reference of the reversed original array.

Example 1: The below examples illustrate the JavaScript Array reverse() Method

JavaScript




function func() {
    // Original Array
    let arr = ['Portal', 'Science',
        'Computer', 'neveropen'];
    console.log(arr);
 
    // Reversed array
    let new_arr = arr.reverse();
    console.log(new_arr);
}
func();


Output

[ 'Portal', 'Science', 'Computer', 'neveropen' ]
[ 'neveropen', 'Computer', 'Science', 'Portal' ]

Example 2: 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:

  • 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

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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments