Adds new elements to the end of the array, and returns the new length of the array.
Method of Array
Syntax
push(element1, ..., elementN )
Parameters
Parameter | Description |
---|---|
element1, ..., element_N | The elements to add to the end of the array. |
Description
This method adds its arguments in order to the end of the array, changing the length of the array. The new length of the array is returned.
If one of the arguments is an array, a reference to it is added as an element (if a referenced object changes, the changes will be visible to both the new array element and original argument array; both the original argument array and the new array element refer to the same object). Use the Array.concat method to join the elements of two or more arrays.
Example
The following example creates the fish array containing two elements, then adds three elements to it.
After the code executes, pushed is set to 5. fish[0] is "angel", fish[1] is "clown", fish[2] is "drum", fish[3] is "lion", and fish[4] is a reference to the moreFish array, so that fish[4][0] is "mandarin" and fish[4][1] is "sturgeon".