push()

The push() method is used to add entries to the given array by "pushing" them to the end of the array. Examine the example and syntax to get a fell for this method.

syntax:

Array.push(argumentOne, argumentTwo, etc.)

arrayName.push(argumentOne, argumentTwo, etc.)

EXAMPLE

var newArray = new Array(10, 20);



document.write("The contents of the array are : " + newArray[0] + " and " + newArray[1] + "
");



newArray.push(30);



document.write("The contents of the array are now : " + newArray[0] + ", " + newArray[1] + " and " + newArray[2] );

The example begins with the creation of a new array called newArray. newArray contains two numbers, 10 and 20. A document.write statement is used to write the contents of the array to the screen. The array is then pushed with a new value, which is 30, and another document.write statement is used to write the new contents of newArray. The output looks like the following:



The contents of the array are : 10 and 20

The contents of the array are now : 10, 20 and 30