how to reverse the a array in javascript code example
Example 1: reversing an array in js
var arr=[1,2,5,6,2]
arr.reverse()
Example 2: array reverse algorithm in js
let array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
let array2 = [5,8,2,9,5,6,3,1];
function reverseArray(arr) {
var newArray = [];
for (var i = arr.length - 1; i >= 0; i--) {
newArray.push(arr[i]);
}
return newArray;
}
reverseArray(array1);
reverseArray(array2);
Example 3: reverse an array javascript
const reversed = array1.reverseconst reversed = array1.reverse();
console.log('reversed:', reversed);
console.log('reversed:', reversed);
console.log('reversed:', reversed);