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.

Practical Learning Notes for Reverse an Array

This JavaScript program is part of the "Array Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents