Reverse an Array

Program to reverse array elements

BeginnerTopic: Array Programs
Back

JavaScript Reverse an Array Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
// Method 1: Using reverse() method (mutates original)
let arr1 = [1, 2, 3, 4, 5];
arr1.reverse();
console.log("Using reverse():", arr1);

// Method 2: Using reverse() without mutation (spread operator)
let arr2 = [1, 2, 3, 4, 5];
let reversed = [...arr2].reverse();
console.log("\nOriginal:", arr2);
console.log("Reversed (new array):", reversed);

// Method 3: Using for loop
function reverseArray(arr) {
    let reversed = [];
    for (let i = arr.length - 1; i >= 0; i--) {
        reversed.push(arr[i]);
    }
    return reversed;
}

console.log("\nUsing loop:", reverseArray([10, 20, 30, 40]));

// Method 4: Using while loop with two pointers
function reverseArrayTwoPointers(arr) {
    let left = 0;
    let right = arr.length - 1;
    let reversed = [...arr];
    
    while (left < right) {
        // Swap elements
        [reversed[left], reversed[right]] = [reversed[right], reversed[left]];
        left++;
        right--;
    }
    return reversed;
}

console.log("\nTwo pointers:", reverseArrayTwoPointers([1, 2, 3, 4, 5]));

// Method 5: Using reduce
function reverseArrayReduce(arr) {
    return arr.reduce((acc, item) => [item, ...acc], []);
}

console.log("\nUsing reduce:", reverseArrayReduce(['a', 'b', 'c', 'd']));

// Method 6: In-place reversal (mutates original)
function reverseInPlace(arr) {
    let left = 0;
    let right = arr.length - 1;
    
    while (left < right) {
        [arr[left], arr[right]] = [arr[right], arr[left]];
        left++;
        right--;
    }
    return arr;
}

let original = [1, 2, 3, 4];
console.log("\nIn-place:", reverseInPlace(original));
console.log("Original mutated:", original);
Output
Using reverse(): [ 5, 4, 3, 2, 1 ]

Original: [ 1, 2, 3, 4, 5 ]
Reversed (new array): [ 5, 4, 3, 2, 1 ]

Using loop: [ 40, 30, 20, 10 ]

Two pointers: [ 5, 4, 3, 2, 1 ]

Using reduce: [ 'd', 'c', 'b', 'a' ]

In-place: [ 4, 3, 2, 1 ]
Original mutated: [ 4, 3, 2, 1 ]

Understanding Reverse an Array

This program demonstrates different methods to reverse an array.

Method 1: Built-in reverse()

JavaScript's native method:

arr.reverse();

Important:

Mutates original array!

Method 2: Non-mutating reverse()

Create copy first:

let reversed = [...arr].reverse();

Spread Operator:

Creates shallow copy
Original array unchanged

Method 3: For Loop

Iterate backwards:

for (let i = arr.length - 1; i >= 0; i--) {
    reversed.push(arr[i]);
}

Method 4: Two-Pointer Technique

Swap from both ends:

while (left < right) {
    [arr[left], arr[right]] = [arr[right], arr[left]];
    left++;
    right--;
}

Destructuring Assignment:

Swaps values in one line
No temporary variable needed

Method 5: Reduce

Functional approach:

arr.reduce((acc, item) => [item, ...acc], []);

How it works:

Starts with empty array
Prepends each item to accumulator
Result is reversed

Method 6: In-Place Reversal

Mutates original array:

function reverseInPlace(arr) {
}
    // swaps elements
    return arr; // original is modified

When to Mutate:

-

Mutate

: Performance critical, original not needed

-

Don't mutate

: Keep original, functional programming

Time Complexity:

All methods: O(n)
Two-pointer: Most efficient (n/2 swaps)

Space Complexity:

Copy methods: O(n)
In-place: O(1)

When to Use:

-

reverse()

: Simplest, if mutation OK

-

Spread + reverse()

: Non-mutating, simple

-

Two-pointer

: Efficient, in-place

-

Reduce

: Functional style

Let us now understand every line and the components of the above program.

Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.

Table of Contents