Array.push() (Method)

Pushes items onto 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:Number primitive
JavaScript syntax:-myArray.push(someValue, ...)
Argument list:someValueA series of values to be pushed onto the stack.

The value as added to the end of the array.

If the value is an array itself, it is not flattened. When it is eventually popped, you get the array back.

If several values are passed to the push() method, they will all be added to the stack, but only the last one will be returned.

This modifies the receiving array, increasing the array length by the number of elements that were pushed onto the end.

The result of this method is the new length of the receiving array after the pushed item has been concatenated onto its tail.

Example code:

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

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

   document.write("Array<BR>")

   displayArrayAsTable(myArray);

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

   document.write(myArray.push("XXX"))

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

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

insert figure 0104

Cross-references:

ECMA 262 edition 3 - section - 15.4.4.7