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




unshift() - FUNCTION


unshift() Function is used to add a new element at the beginning of the array and shifts other elements to the right.


Let us say, we have a Array that contains four names, Mohan, Kriti, Philip and Salim. And we want to add the element Benny at the beginning of the Array.


Example :



<html>
<body>  
<script>

	var x = ["Mohan", "Kriti", "Philip", "Salim"]
	x.unshift("Benny")
	document.write(x)
    
</script>      
</body>
</html>


Output :



  Benny,Mohan,Kriti,Philip,Salim

So, all we have done is, used the unshift() method to add the name Benny at the beginning of the Array.


x.unshift("Benny")

And what happens is Benny gets inserted at index 0 pushing all other elements to the right.