=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 >Web Front-end >JS Tutorial >How to reverse an array in javascript

How to reverse an array in javascript

青灯夜游
青灯夜游Original
2021-12-16 18:38:4010569browse

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()".

How to reverse an array in javascript

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);

How to reverse an array in javascript

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);

How to reverse an array in javascript

[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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn