Home Java Javascript-array JavaScript array shift



JavaScript array shift
Posted on: November 5, 2008 at 12:00 AM
In the JavaScript array there is one method shift() which has the reverse functionality of pop() method. shift() method removes the first element from the array and also returns this removed element.

JavaScript array shift

     

In the JavaScript array there is one method shift() which has the reverse functionality of pop() method. shift() method removes the first element from the array and also returns this removed element.

Syntax of shift() method is as given below:

arrayObject.shift();

It removes first element from the array and as well as changes the length of the array. Here in the following example code we have declared a JavaScript array of length three and it contains three elements. We have operated the shift() method on this array. Here is the full example code for this example as follows:

 

 

<html>
<head>
<title>
JavaScript array shift() example
</title>
<script type="text/javascript">
var arr = new Array(3);
arr[0]="Sandeep";
arr[1]="Suman";
arr[2]="Saurabh";
   document.write("<b>Array before shift() is </b>=>"+arr+"<br> ");
   document.write("<b>Shifted element is </b>=>"+arr.shift()+"<br> ");
   document.write("<b>Array after shift() is</b>=>"+arr+"<br> ");
</script>
</head>
<body bgcolor="#ddcdfd">
<h2>
Example of Shifting array elements
</h2>
</body>
</html>

Output :

Download Sample Source Code

Related Tags for JavaScript array shift:
javajavascriptcarrayfunctionscriptfuniomethodremoveipreturnthiselementfunctionalmovereverseifuncsheitshiftlslipopfromfirstinasmntjesemfuncmewhichsurnsoisirharayfunctionalityandarvascrssriripthavstlsomovndonomo


More Tutorials from this section

Ask Questions?    Discuss: JavaScript array shift  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.