Table of Contents
What reverse() Does
Flips the order of the items: the first becomes last, the second becomes second to last, and so on.
Changes the original array: it reverses in place, so the array you called it on is modified.
Also returns the array: the same array, now reversed, not a copy.
Takes no arguments: you just call it.
let numbers = [1, 2, 3, 4, 5]; numbers.reverse(); console.log(numbers); // [5, 4, 3, 2, 1]
Syntax
array.reverse();
No parameters: there's nothing to pass in.
Return value: the reversed array, which is the same array object you started with.
let letters = ["a", "b", "c"]; let result = letters.reverse(); console.log(result); // ["c", "b", "a"] console.log(result === letters); // true, same array
Practical Examples
Reversing a list of names:let names = ["Vi", "Sam", "Alex"]; console.log(names.reverse()); // ["Alex", "Sam", "Vi"]Showing the newest item first:
let posts = ["Post 1", "Post 2", "Post 3"]; console.log([...posts].reverse()); // ["Post 3", "Post 2", "Post 1"]Sorting in descending order after sorting ascending:
let nums = [10, 2, 33, 4]; nums.sort((a, b) => a - b).reverse(); console.log(nums); // [33, 10, 4, 2]
Reversing a String
Strings don't have a reverse method: but you can borrow the array one.
let word = "hello";
let reversed = word.split("").reverse().join("");
console.log(reversed); // "olleh"
How it works: split("") turns the string into an array of characters, reverse() flips it, and join("") turns it back into a string.
A common interview question: checking for a palindrome uses exactly this pattern.
function isPalindrome(text) {
let cleaned = text.toLowerCase();
return cleaned === cleaned.split("").reverse().join("");
}
console.log(isPalindrome("Level")); // true
Things to Remember
It changes the original array: copy it first if you need to keep the original order.let original = [1, 2, 3]; let flipped = [...original].reverse(); console.log(original); // [1, 2, 3] console.log(flipped); // [3, 2, 1]There's a non-mutating version: toReversed() returns a new reversed array and leaves the original alone, in newer JavaScript environments.
let flipped2 = original.toReversed();It reverses only the top level: nested arrays keep their own internal order.
let nested = [[1, 2], [3, 4]]; console.log(nested.reverse()); // [[3, 4], [1, 2]]It doesn't sort anything: it just flips the current order, whatever that is.
Common Mistakes
Forgetting that it changes the original: this causes bugs when the same array is used elsewhere.
Expecting it to sort in descending order: it only flips the current order. Sort first, then reverse, or use sort((a, b) => b - a).
Calling it on a string directly: strings don't have reverse(). Split into an array first.
Conclusion
reverse() is simple: it flips an array in place. Just remember that it changes the original, so copy the array first with the spread operator, or use toReversed(), whenever you still need the original order.Frequently Asked Questions (FAQs)
1.Does reverse() change the original array?2.How do I reverse an array without changing the original?Yes. It reverses in place and returns the same array.
3.How do I reverse a string in JavaScript?Use [...array].reverse() or array.toReversed().
Use string.split("").reverse().join("").
0 Comments