Array.pop() (Method)

Pops items off of the end of an array like a FILO stack.

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 the array content
JavaScript syntax:-myArray.pop()

The pop() method returns the element at the end of the array. In doing so, it deletes the item from the end of the array reducing the array length by one.

Elements are returned one at a time, even if several were pushed onto the stack together.

Arrays that were pushed onto the stack are returned as arrays.

Although this is very useful for programming stacks, it is not portable enough to deploy in a public facing site.

The result of this method is the item that was on the end of the stack.

Example code:

   // Create an array and test the Array.pop() method

   myArray = new Array("AAA", "BBB", "CCC");

   document.write("Array<BR>")

   displayArrayAsTable(myArray);

   document.write("Array.pop()<BR>")

   document.write(myArray.pop())

   document.write("<BR><BR>")

   document.write("Array after pop() 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.push(), Queue manipulation, Stack manipulation

insert figure 0103

Cross-references:

ECMA 262 edition 3 - section - 15.4.4.6