w3resource

JavaScript reverse()  Method: Array Object

Description

The reverse() method is used to reverse the order of the elements in an array, i.e. first array element becomes the last and the last becomes the first.

Version

Implemented in JavaScript 1.1

Syntax

reverse()

Parameters

None

Example:

In the following web document there is an array fruitslist containing four elements, then reverse() method is applied on the array.

HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8" />
<title>JavaScript reverse() method example</title>
<style type="text/css">
h1 {color:red}
</style>
</head>
<body>
<h1>JavaScript : reverse() method</h1>
<script src="array-reverse-example1.js"></script>
</body>
</html>

JS Code

var fruitslist = new Array("Orange","Apple","Banana","Chery" );

var newParagraph = document.createElement("p");
var newText = document.createTextNode("List of Fruits : " + fruitslist); 
newParagraph.appendChild(newText); 
document.body.appendChild(newParagraph); 

var newfruitslist=fruitslist.reverse();

var newParagraph1 = document.createElement("p"); 
var newText1 = document.createTextNode("New List of Fruits : " + newfruitslist);
newParagraph1.appendChild(newText1); 
document.body.appendChild(newParagraph1);
 

View the example in the browser

Practice the example online

JavaScript reverse() method example

See also:

JavaScript Core objects, methods, properties.

Previous: JavaScript push() Method: Array Object
Next: JavaScript shift() Method: Array Object

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.