Definition and Usage
The reverse() function for arrays reverses the order of the elements in the given array.
Features
- This function modifies the original array in place.
- The first element of the array becomes the last, and the last element becomes the first.
- It does not return a new array.
Basic Example
const arr = [1, 2, 3, 4, 5];
const reversedArr = arr.reverse();
/* Reverses the order of elements in the array */
console.log(reversedArr); // Output: [5, 4, 3, 2, 1]
/* Modifies the original array in place */
console.log(arr); // Output: [5, 4, 3, 2, 1]
Syntax
arr.reverse()
arr is the array to which the reverse() function is applied.
Parameters
None.
Return value
Reverses the elements of the original array in place and returns the modified array. The return value is the original array itself after modification. It does not return a new array.
Things to Keep in Mind
The reverse() function modifies the original array in place.
That is, calling reverse() reverses the elements of the original array itself. It does not create a new array or a copy. Therefore, you should be aware that the original array will be changed when using this function. If you want to reverse the elements while keeping the original array intact, create a copy of the array before applying reverse(), or use the toReversed() function, which returns a new array with the elements in reverse order without modifying the original array.
Creating a Copy of the Original Array Using slice()
Additional Explanation
The slice() function for arrays extracts a portion of an array based on a specified index range and returns a new array.
const originalArray = [1, 2, 3, 4, 5];
// Create a copy of the original array
const copiedArray = originalArray.slice();
// Apply the reverse() function to the copy
copiedArray.reverse();
console.log(originalArray); // [1, 2, 3, 4, 5] (the original array remains unchanged)
console.log(copiedArray); // [5, 4, 3, 2, 1] (the array is reversed)
Creating a Copy of the Original Array Using the Spread (...) Syntax
Additional Explanation
The spread (...) syntax is a JavaScript feature that allows you to expand the elements of an iterable object (such as an array, string, or DOM collection) into individual elements. It is used by placing an iterable object (array, string, DOM collection, etc.) after the ... three dots.
const originalArray = [1, 2, 3, 4, 5];
// Create a copy of the original array using the spread (...) syntax
const copiedArray = [...originalArray];
// Apply the reverse() function to the copy
copiedArray.reverse();
console.log(originalArray); // [1, 2, 3, 4, 5] (the original array remains unchanged)
console.log(copiedArray); // [5, 4, 3, 2, 1] (the array is reversed)
Using the toReversed() Function
The toReversed() function works exactly like the reverse() function, but it does not modify the original array.
The toReversed() function was introduced in ECMAScript 2023 to address the limitations of reverse(). Previously, the reverse() function was the only way to reverse the order of elements in an array.
Since reverse() modifies the original array, it cannot be used when you need to preserve the original elements. The toReversed() function creates a new array with the elements reversed, solving this problem.
const originalArray = [1, 2, 3, 4, 5];
/* Reverses the order of the elements in the array */
const reversedArray = originalArray.toReversed();
console.log(reversedArray); // [5, 4, 3, 2, 1] (the array is reversed)
/* The original array remains unchanged */
console.log(originalArray); // [1, 2, 3, 4, 5]
Note:
The toReversed() function was introduced in 2023, so you should check browser compatibility before using it.
| Method |
Desktop Chrome
|
DesktopDesktop Edge
|
Desktop Firefox
|
Safari
|
Node.js
|
|---|---|---|---|---|---|
toReversed()
|
110 | 110 | 115 | 16 | 20 |
Practical Examples
The reverse() function can be used in various situations. Let's take a look at a few examples.
Reversing and Iterating
const numbers = [1, 2, 3, 4, 5];
// Reverse the array order and print each element
numbers.reverse().forEach(number => {
console.log(number);
});
// Output: 5, 4, 3, 2, 1
Code Explanation
The forEach() function is used to loop through an array and process each element with a callback function.
Reversing a String
const str = "Hello, World!";
// Convert the string to an array, reverse it, then join it back to a string
const reversedStr = str.split("").reverse().join("");
console.log(reversedStr);
// Output: "!dlroW ,olleH"
Code Explanation
The split() function for strings splits a string into an array based on a given delimiter or regular expression.
Code Explanation
The sort() function for arrays is used to rearrange the elements of an array in a desired order.
Code Explanation
The join() function for arrays joins the elements of an array into a single string, using a specified separator.
Reversing and Filtering
const numbers = [1, 2, 3, 4, 5];
// Reverse the array and filter out the elements that meet a condition
const filteredNumbers = numbers.reverse().filter(number => number % 2 === 0);
console.log(filteredNumbers);
// Output: [4, 2]
Code Explanation
The filter() function for arrays filters elements of an array using a callback function.
Compatibility
| Method |
Desktop Chrome
|
DesktopDesktop Edge
|
Desktop Firefox
|
Safari
|
Node.js
|
|---|---|---|---|---|---|
reverse()
|
1 | 12 | 1 | 1 | 0.10 |
Specifications
| Specification | |
|---|---|
reverse()
|
ECMAScript® 2026 Language Specification #sec-array.prototype.reverse |
See also
- JavaScript Array find() Function – Finding the First Element That Satisfies the Callback Condition
- JavaScript Array findIndex() Function – Finding an Element’s Index with a Callback
- JavaScript Array indexOf() Function – Find the First Index of an Element
- JavaScript Array includes() Function – Checking for an Element