var arr = [ "I", "learn" ]; // Array with just 2 elements
var newLength = arr.push("JavaScript"); // Add 1 and capture new size
alert( "Array contains " + newLength + " elements:\n" + arr.join("-") );
Note: last line uses join() method to clearly show each element (separated with a hyphen).
var arr = [ "I", "learn" ];
arr.push( "JavaScript", "and", "it", "is", "fun!" );
alert( "Updated array's elements:\n" + arr.join("-") );
$arr = array( 'I', 'learn' );
// Append 4 new elements and count the number added:
$addedElementsCount = $arr.array_push( $arr, 'PHP', 'which', 'is', 'fun!' );
echo 'The updated array size is: ' . count( $arr ) . '<br>'; // 6
print_r( $arr ); // Show each element
Sister methods - array insertion and deletion:
• concat() adds elements or arrays at the end of an array, and returns a copy of the new array.
• unshift() adds an element to the start of an array ("prepends"), and returns the new length.
• push() adds an element to the end of an array ("appends"), and returns the new length.
• shift() removes the first element of an array, and returns its value.
• pop() removes the last element of an array, and returns its value.