| home / programming / java_core / 1 | [previous] [next] |
|
|
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.
|
|
|---|
An Array object called names is declared and intialized. |
The shift() method removes the first element of an array and returns the value shifted off; the unshift() method adds elements to the beginning of the array. These methods are just like pop() and push() except that they manipulate the beginning of the array instead of the end of it.
|
var return_value=Arrayname.shift(); Arrayname.shift( new elements); // Prepended to the array |
|
|
|---|
A new Array object called names is created. |
The slice() method copies elements of one array into a new array. The slice() method takes two arguments: the first number is the starting element in a range of elements that will be copied, and the second argument is the last element in the range, but this element is not included in what is copied. Remember that the index starts at zero, so that a beginning position of 2 is really element 3. The orginal array is unaffected unless you assign the result of the slice back to the original array.
|
var newArray = Arrayname.slice(first element, last element); var ArraySlice = myArray.slice(2,6); // ArraySlice contains elements |
This is the original array of names. |
The splice() method [not to be confused with slice()] removes a specified number of elements from some starting position in an array and allows you to replace those items with new ones. (Don't confuse this method with the slice() method. Ropes, tapes, and films are spliced; bread, meat, and golf balls are sliced.)
|
Arrayname.splice( index position, number of elements to remove); Arrayname.splice(index position, number of elements to remove, |
| home / programming / java_core / 1 | [previous] [next] |
Created: March 27, 2003
Revised: November 19, 2003
URL: http://webreference.com/programming/java_core/1