Array.shift() (Method)

Pull off of a stack whose access is FILO from the start rather than the end.

Availability:

ECMAScript edition - 3
JavaScript - 1.2
JScript - 5.5
Internet Explorer - 5.5
Netscape - 4.0
Netscape Enterprise Server - 3.0
Property/method value type:Depends on array content
JavaScript syntax:-myArray.shift()

This method pulls an item from the front of the stack and removes that item.

The array elements are all moved down one index position.

This modifies the array in place.

The result of this method is the item that is deleted from the front of the stack.

Example code:

   // Create an array and test the Array.shift() method
   myArray = new Array("AAA", "BBB", "CCC");
   document.write("Array<BR>")
   displayArrayAsTable(myArray);
   document.write("Array.shift()<BR>")
   document.write(myArray.shift())
   document.write("<BR>")
   document.write("<BR>")
   document.write("Array after shift() call<BR>")
   displayArrayAsTable(myArray);
   
   // Display an array in a table
   function displayArrayAsTable(anArray)
   {
      myLength = anArray.length;
      document.write("<TABLE BORDER=1>");
      for(myIndex = 0; myIndex < myLength; myIndex++)
      {
         document.write("<TR><TD>");
         document.write(myIndex);
         document.write("</TD><TD>");
         document.write(anArray[myIndex]);
         document.write("</TD></TR>");
      }
      document.write("</TABLE><BR><BR>")
   }

See also:Array.prototype, Array.unshift(), Queue manipulation, Stack manipulation

insert figure 0105

Cross-references:

ECMA 262 edition 3 - section - 15.4.4.9