var arr = [ "I", "learn", "JavaScript" ];
var oldLastElement = arr.pop(); // Remove last
// Get value of former last, and new array size:
alert( oldLastElement + "\nNew number of elements = " + arr.length );
Tip - to delete all elements, just reset the variable's value to a new, empty array: arr = [];
$arr = array( 'I', 'learn', 'PHP' );
$oldLastElement = array_pop( $arr );
// Snippet below writes: Element 'PHP' removed - new length = 2
echo "Element '$oldLastElement' removed - new length = " . count($arr);
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.