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

黄舟
黄舟Original
2017-11-03 11:22:354243browse

Definition and Usage

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

Syntax

arrayObject.reverse()

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>

Output:

George,John,Thomas
Thomas,John,George

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

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>

This will produce the following results:

Reversed array is : 3,2,1,0


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!

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