Understanding the JavaScript reverse() Method

The reverse() method in JavaScript is used to reverse the order of elements in an array. This method modifies the original array by reversing its elements, meaning the first element becomes the last and the last becomes the first.

The reverse() method…


This content originally appeared on DEV Community and was authored by Sudhanshu Gaikwad

The reverse() method in JavaScript is used to reverse the order of elements in an array. This method modifies the original array by reversing its elements, meaning the first element becomes the last and the last becomes the first.

The reverse() method overwrites the original array with the elements in reverse order. It does not create a new array but rather changes the existing one. This is a useful method when you need to invert the sequence of elements for tasks such as sorting in reverse order or simply changing the order for display purposes.

1. Simple Example
Here's a simple example demonstrating the use of the reverse() method:

let fruits = ["Apple", "Banana", "Cherry", "Date"];
console.log("Original array:", fruits);

// Here's i have used reverse() method
fruits.reverse();
console.log("Reversed array:", fruits);

Output:

Original array: [ 'Apple', 'Banana', 'Cherry', 'Date' ]
Reversed array: [ 'Date', 'Cherry', 'Banana', 'Apple' ]

2.Example
let's create an index.jsfile and I write code.

 var data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
      data.reverse();
      console.log(data);

Output:

Image description


This content originally appeared on DEV Community and was authored by Sudhanshu Gaikwad


Print Share Comment Cite Upload Translate Updates
APA

Sudhanshu Gaikwad | Sciencx (2024-07-25T06:22:54+00:00) Understanding the JavaScript reverse() Method. Retrieved from https://www.scien.cx/2024/07/25/understanding-the-javascript-reverse-method/

MLA
" » Understanding the JavaScript reverse() Method." Sudhanshu Gaikwad | Sciencx - Thursday July 25, 2024, https://www.scien.cx/2024/07/25/understanding-the-javascript-reverse-method/
HARVARD
Sudhanshu Gaikwad | Sciencx Thursday July 25, 2024 » Understanding the JavaScript reverse() Method., viewed ,<https://www.scien.cx/2024/07/25/understanding-the-javascript-reverse-method/>
VANCOUVER
Sudhanshu Gaikwad | Sciencx - » Understanding the JavaScript reverse() Method. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/25/understanding-the-javascript-reverse-method/
CHICAGO
" » Understanding the JavaScript reverse() Method." Sudhanshu Gaikwad | Sciencx - Accessed . https://www.scien.cx/2024/07/25/understanding-the-javascript-reverse-method/
IEEE
" » Understanding the JavaScript reverse() Method." Sudhanshu Gaikwad | Sciencx [Online]. Available: https://www.scien.cx/2024/07/25/understanding-the-javascript-reverse-method/. [Accessed: ]
rf:citation
» Understanding the JavaScript reverse() Method | Sudhanshu Gaikwad | Sciencx | https://www.scien.cx/2024/07/25/understanding-the-javascript-reverse-method/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.