=0;i--){nar.push(arr[i ])}"; 2. Use reverse(), syntax "arr.reverse()"."/> =0;i--){nar.push(arr[i ])}"; 2. Use reverse(), syntax "arr.reverse()".">
Home > Article > Web Front-end > How to reverse an array in javascript
Reversal method: 1. Use for loop and push(), the syntax "var nar=[];for(var i=arr.length-1;i>=0;i--){nar .push(arr[i])}"; 2. Use reverse(), syntax "arr.reverse()".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Javascript method to reverse an array
1. Use for loop and push() method
var arr = [1,2,3,4,5,6]; var newArr=[]; for(var i=arr.length-1;i>=0;i--){ newArr.push(arr[i]); } console.log(newArr);
2. Use the reverse() method
The reverse() method is used to reverse the order of elements in the array.
Grammar:
array.reverse()
Example:
var arr = [1,2,3,4,5,6,7,8]; var newArr=arr.reverse(); console.log(newArr);
[Related recommendations: javascript learning tutorial】
The above is the detailed content of How to reverse an array in javascript. For more information, please follow other related articles on the PHP Chinese website!