Page 148 - DCAP408_WEB_PROGRAMMING
P. 148
Web Programming
Notes pop() Removes the last element of an array, and returns that element
push() Adds new elements to the end of an array, and returns the new length
reverse() Reverses the order of the elements in an array
shift() Removes the first element of an array, and returns that element
slice() Selects a part of an array, and returns the new array
sort() Sorts the elements of an array
splice() Adds/Removes elements from an array
toString() Converts an array to a string, and returns the result
unshift() Adds new elements to the beginning of an array, and returns the new length
valueOf() Returns the primitive value of an array
Example: Create an array, assign values to it, and write the values to the output.
<html>
<body>
<script type=”text/javascript”>
var mycars = new Array();
mycars[0] = “mercedes”;
mycars[1] = “ferari”;
mycars[2] = “BMW”;
mycars[3] = “opel”;
mycars[4] = “honda city”;
for (i=0;i<mycars.length;i++)
{
document.write(mycars[i] + “<br/>”);
}
</script>
</body>
</html>
Output
mercedes
ferari
BMW
opel
honda city
Self Assessment
Fill in the blanks:
10. To modify a value in an existing array, just add a new value to the array with a specified
………………..
11. An array can hold all your variable values under a ……………….. name.
142 LOVELY PROFESSIONAL UNIVERSITY