OneBite.Dev - Coding blog in a bite size

reverse arrays items in Javascript

Code snippet on how to reverse arrays items in Javascript

  function reverseArray(arr) {
    let newArray = [];
    
    for (let i = arr.length - 1; i >= 0; i--) {
      newArray.push(arr[i]);
    }
    
    return newArray;
  }

This code is a function that can reverse an array of elements. It takes an array as an argument, then creates a new, empty array. After that, it starts a loop which iterates through the input array in reverse order (starting from the last element). For each item of the array, it adds it to the new array. Finally, it returns the new array which contains the elements of the input array, just in reverse order.

javascript