Skip to content Skip to sidebar Skip to footer

Javascript Reverse An Array Without Using Reverse()

I want to reverse an array without using reverse() function like this: function reverse(array){ var output = []; for (var i = 0; i<= array.length; i++){ output.

Solution 1:

array.pop() removes the popped element from the array, reducing its size by one. Once you're at i === 4, your break condition no longer evaluates to true and the loop ends.


One possible solution:

function reverse(array) {
  var output = [];
  while (array.length) {
    output.push(array.pop());
  }

  return output;
}

console.log(reverse([1, 2, 3, 4, 5, 6, 7]));

Solution 2:

You can make use of Array.prototype.reduceright and reverse it

check the following snippet

var arr = ([1, 2, 3, 4, 5, 6, 7]).reduceRight(function(previous, current) {
  previous.push(current);
  return previous;
}, []);

console.log(arr);

Solution 3:

No need to pop anything... Just iterate through the existing array in reverse order to make your new one.

function reverse(array){
    var output = [];
    for (var i = array.length - 1; i> -1; i--){
        output.push(array[i]);
    }

    return output;
}

console.log(reverse([1,2,3,4,5,6,7]));

Edit after answer got accepted.

A link in a comment on your opening post made me test my way VS the accepted answer's way. I was pleased to see that my way, at least in my case, turned out to be faster every single time. By a small margin but, faster non the less.

Here's the copy/paste of what I used to test it (tested from Firefox developer scratch pad):

function reverseMyWay(array){
    var output = [];
    for (var i = array.length - 1; i> -1; i--){
        output.push(array[i]);
    }

    return output;
}

function reverseTheirWay(array) {
  var output = [];
  while (array.length) {
    output.push(array.pop());
  }

  return output;
}

function JustDoIt(){
    console.log("their way starts")
    var startOf = new Date().getTime();
    for(var p = 0; p < 10000; p++)
        {
            console.log(reverseTheirWay([7,6,5,4,3,2,1]))
        }
    var endOf = new Date().getTime();
    console.log("ran for " + (endOf - startOf) + " ms");
    console.log("their way ends")

}


function JustDoIMyWay(){
    console.log("my way starts")
    var startOf = new Date().getTime();
    for(var p = 0; p < 10000; p++)
        {
            console.log(reverseMyWay([7,6,5,4,3,2,1]))
        }
    var endOf = new Date().getTime();
    console.log("ran for " + (endOf - startOf) + " ms");
    console.log("my way ends")
}

JustDoIt();
JustDoIMyWay();

Solution 4:

In ES6 this could be written as

reverse = (array) => array.map(array.pop, [... array]);

Solution 5:

Solution to reverse an array without using built-in function and extra space.

let arr = [1, 2, 3, 4, 5, 6, 7];
let n = arr.length-1;

for(let i=0; i<=n/2; i++) {
  let temp = arr[i];
  arr[i] = arr[n-i];
  arr[n-i] = temp;
}
console.log(arr);

Post a Comment for "Javascript Reverse An Array Without Using Reverse()"