Array push, pop, and length demo

This demonstration enables studnets to experiment with the Array object and some of its methods.

JavaScript

// Create an empty array and add different kinds of vegetables
var vegetable = new Array();
vegetable[0] = "peas";
vegetable[1] = "corn";
vegetable[2] = "carrot";

vegetable.push("apple");
alert(vegetable.toString());

vegetable.pop("peas");
alert(vegetable.toString());

/* Try this:
1. replace line 2 with var vegetable = [ ];
2. Replace lines 3 through 4 so that they use the push() method of the Array object instead of hardcoding array subscripts.
3. Call alert and display the length of the array.
4. Call alert and display the last element of the array.
*/