×

Index

JavaScript Tutorial

JavaScript Examples

CSS Tutorial

CSS Examples

Array reverse() method with example in JavaScript

JavaScript reverse() method: Here, we are going to learn about the reverse() method of array in JavaScript.
Submitted by IncludeHelp, on February 28, 2019

JavaScript reverse() method

reverse() method is used to reverse the order of the elements in an array, it changes the actual array and also returns an array with reversed elements.

Syntax

array_name.reverse();

Parameters

None

Return value

Array with reversed elements.

Sample Input/Output

Input:
var arr1 = [10, 20, 30, 40, 50];

Function call:
arr1.reverse();
    
Output:
50,40,30,20,10

JavaScript Code to demonstrate example of Array.reverse() method

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<script>
		var arr1 = [10, 20, 30, 40, 50];
		var arr2 = ["Amit", "Ankur", "Akash"];
		
		//printing arrays before the reversing elements
		document.write("arr1: " + arr1 + "<br>");
		document.write("arr1 length: " + arr1.length + "<br>");
		document.write("arr2: " + arr2 + "<br>");
		document.write("arr2 length: " + arr2.length + "<br>");
		
		//reversing arrays
		arr1.reverse();
		arr2.reverse();
		
		//printing
		document.write("<br>After reversing ...<br><br>");
		document.write("arr1: " + arr1 + "<br>");
		document.write("arr2: " + arr2 + "<br>");
		
	</script>
</body>
</html>

Output

arr1: 10,20,30,40,50
arr1 length: 5
arr2: Amit,Ankur,Akash
arr2 length: 3

After reversing ...

arr1: 50,40,30,20,10
arr2: Akash,Ankur,Amit

JavaScript Array Object Methods »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.