Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › JavaScript › Reference › reverse()

JavaScript Reference

Array.prototype.reverse()

Flip the order of elements in place. Returns the same array reference.

Last updated: October 2026

Definition: Array.prototype.reverse() reverses the elements of an array in place and returns a reference to the same array. The first element becomes the last, and the last becomes the first [citation:15].

🎬 Live Example

Five reverse patterns. Click any button to see the output.

Input:

                
Output:

            

💡 reverse mutates in place and returns the same array reference.

📝

Basic Syntax

array.reverse()

// No arguments
// Returns the SAME array reference (reversed in place)
// MUTATES the original array
1

Basic Array Reversal

const nums = [1, 2, 3, 4, 5];
nums.reverse();
// nums: [5, 4, 3, 2, 1]
2

The Mutation Trap

const original = [1, 2, 3];
const reversed = original.reverse();

console.log(reversed);  // [3, 2, 1]
console.log(original);  // [3, 2, 1]  ← ALSO reversed!

// They are the SAME array reference

Critical: reverse returns the same array, not a copy. If you need the original intact, copy first.

3

Reverse a String

const str = 'Hello';

// Chain: split → reverse → join
str.split('').reverse().join('');
// 'olleH'

// Reverses characters, not words

Pro tip: This is the classic JavaScript string reversal idiom. Strings are immutable, so you must convert to array first.

4

Non-Destructive Reverse

const original = [1, 2, 3];

// ✅ Copy first
const reversed = [...original].reverse();
// original: [1, 2, 3]
// reversed: [3, 2, 1]

// ✅ ES2023+ (modern browsers)
const newer = original.toReversed();
5

Real-World: Newest-First Log

const logs = [];

function addLog(msg) {
    logs.push(msg);
}

addLog('start');
addLog('process');
addLog('done');

// Show newest first
const newestFirst = [...logs].reverse();
// ['done', 'process', 'start']

// Or mutate if you don't need original order
logs.reverse();

🎯 reverse vs toReversed vs sort

Method Mutates? Use when
reverseYesIn-place reversal
toReversedNoImmutable reversal (ES2023+)
sortYesOrdered sorting (not reversal)

⚠️ Common Mistakes

❓ Frequently Asked Questions

What does Array.reverse() do in JavaScript?

Array.reverse() reverses the elements of an array in place and returns a reference to the same array. The first element becomes the last, and the last becomes the first.

Does reverse modify the original array?

Yes. reverse mutates the array in place and returns the same array reference. To reverse without mutating, use toReversed() (ES2023+) or spread: [...arr].reverse().

What is the difference between reverse and toReversed?

reverse mutates the original array and returns it. toReversed returns a new reversed array without modifying the original. Use toReversed when you need immutability.

Does reverse work on strings?

No. Strings are immutable and don't have a reverse method. Convert to an array first: str.split('').reverse().join('').

How do I reverse an array without mutating?

Use spread to create a copy first: [...arr].reverse(). Or use arr.slice().reverse(). Or use toReversed() in modern browsers.

🎯 What's Next?

← All JavaScript Reference