// Using the .unshift() method
const reverseArray = arr => {
let reversed = [];
for (let i = 0; i < arr.length; i++) {
reversed.unshift(arr[i]);
}
return reversed
}
if we want to reverse order the last element will become the first.How can this method reverse the order when it starts at first index?
"array.unshift()" is use to push items into array, you need to use "array.reverse()" to reverse the array.
const arr = [1, 2, 3];
arr.unshift(4, 5);
console.log(arr);
// reverse
arr.reverse(arr, "this is unshift");
console.log(arr, "this is reverse");
// output: arr [4, 5, 1, 2, 3]