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.

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.

JavaScript array shift

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