HTML and CSS Reference
In-Depth Information
The pop() Method. The pop() method deletes the last element of an array and
returns the value popped off.
FORMAT
var return_value=Arrayname.pop();
EXAMPLE
var popped = myArray.pop();
EXAMPLE 9.11
<html>
<head><title>Array pop() method</title></head>
<body>
<script type="text/javascript">
1
var names=new Array("Tom", "Dan", "Liz", "Jody");
2
document.write("<b>Original array: "+ names +"<br />");
3
var newstring=names.pop(); // Pop off last element of array
4
document.write("Element popped: "+ newstring );
5
document.write("<br />New array: "+ names + "</b>");
</script>
</body>
</html>
EXPLANATION
1
The Array() constructor creates a new array called names and initializes the array
with four values: “Tom” , “Dan” , “Liz” , and “Jody” .
2
The contents of the array called names is displayed.
3
The last element of the array is removed. The value removed is returned and as-
signed to the variable called newstring .
4
The popped value is displayed.
5
The shortened array is displayed (see Figure 9.14).
Figure 9.14 The Array pop() method.
The push() Method. The push() method adds new elements onto the end of an array,
thereby increasing the length of the array. JavaScript allocates new memory as needed.
 
Search WWH ::




Custom Search