Home > Web Front-end > JS Tutorial > How to reverse an array in javascript

How to reverse an array in javascript

青灯夜游
Release: 2021-12-16 18:38:40
Original
10530 people have browsed it

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);
Copy after login

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()
Copy after login

Example:

var arr = [1,2,3,4,5,6,7,8];
var newArr=arr.reverse();
console.log(newArr);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template