Home Java Javascript-array JavaScript array shift

Related Tutorials


 
 

Share on Google+Share on Google+

JavaScript array shift

Advertisement
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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 5, 2008

Related Tutorials

Discuss: JavaScript array shift  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com