Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




JAVASCRIPT - REVERSE AN ARRAY


How to reverse an Array?


Reversal of an Array can be done using reverse() method. It is independent of the alphabets. And is not a sort. It is just a reversal.


Example :



<html>
<body>  
<script>

	var x = ["Mohan", "Kriti", "Salim"] 
	x.reverse()
	document.write("The Array in reversed order is : [",x,"]")
    
</script>      
</body>
</html>


Output :



  The Array in reversed order is : [Salim, Kriti, Mohan]

So, in the above code we have created a Array and initialised to the variable x.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Array,

java_Collections

Then we have used the reverse() Function to reverse the elements of the Array x.


x.reverse()

And the Array x gets sorted in descending order with Salim as the first value, Mohan second and Kriti as the third.

java_Collections

And we get the below output.


The Array in reversed order is : [Salim, Kriti, Mohan]