Home > Web Front-end > JS Tutorial > JavaScript reverse() method to reverse the order of elements in an array

JavaScript reverse() method to reverse the order of elements in an array

黄舟
Release: 2017-11-03 11:22:35
Original
4202 people have browsed it

Definition and Usage

reverse() method is used to reverse the order of elements in an array.

Syntax

arrayObject.reverse()
Copy after login

Tips and Comments

Comments: This method will change the original array without creating a new array.

Example

In this example, we will create an array and then reverse the order of its elements:

<script type="text/javascript">

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

document.write(arr + "<br />")
document.write(arr.reverse())

</script>
Copy after login

Output:

George,John,Thomas
Thomas,John,George
Copy after login

JavaScript arrayreverse() method reverses array elements. The first array element becomes the last element, and the last element becomes the first element.

The following are the details of the parameters:

NA
Copy after login

Return value:

Returns the reversed single value of the array.
Example:

<html>
<head>
<title>JavaScript Array reverse Method</title>
</head>
<body>
<script type="text/javascript">
var arr = [0, 1, 2, 3].reverse();
document.write("Reversed array is : " + arr ); 
</script>
</body>
</html>
Copy after login

This will produce the following results:

Reversed array is : 3,2,1,0
Copy after login


The above is the detailed content of JavaScript reverse() method to reverse the order of elements in an array. 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