The push () method adds one or more elements to the end of the array and returns the new length. The return value is the new length after the specified value is added to the array.
Syntax: Arrayobject.push (newelement1,newelement2,...., newelementx)
- Parameter newelement1, required. The first element to add to the array.
- Parameter newelement2, optional. The second element to add to the array.
- Parameter newelementx, optional. You can add multiple elements. Luoning County Kindergarten
The push () method adds its parameter order to the tail of the arrayobject. It modifies the arrayobject directly, rather than creating a new array. The push () method and the Pop () method use the advanced post-stack functionality provided by the array. The method changes the length of the array.
View Source print?
1 |
<script type= "text/javascript" > |
6 |
document.write(arr + "<br />" ) |
7 |
document.write(arr.push( "NowaMagic" ) + "<br />" ) |
Program Run Result:
View Source print?
3 |
Apple,Banana,Orange,NowaMagic |
Test the push and pop methods of an array
The following is a simple and practical introduction to the push and pop functions, see the following code:
View Source print?
1 |
<script type= "text/javascript" > |
2 |
var arr = [ "Aplle" , "Banana" , "Orange" , "Pear" ]; |
5 |
var popped = arr2.pop(); |
The push and pop methods of an array object are added and deleted at the end of the array, respectively. The push method has a parameter, which is the element to be added to the end of the array, and the pop method has no parameters, but instead returns the element that was deleted from the end of the array. See the JS code below.
Click on the button above to see "Arr.push" ("Nowamagic"); After one sentence, the elements of the ARR array add one, precisely the nowamagic we push in. Let's take a look at what happened to Arr2 after arr2 called the Pop method.
As can be seen, the ARR2 has changed from 1,2,3,4 to. Because the original last element "4", was pop out. We used the popped variable to store the return value of the pop. You can click the button below to view it.
About the push () function of JavaScript