About the push () function of JavaScript

Source: Internet
Author: User

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">
2 vararr = newArray(3)
3 arr[0] = "Apple"
4 arr[1] = "Banana"
5 arr[2] = "Orange"
6 document.write(arr + "<br />")
7 document.write(arr.push("NowaMagic") + "<br />")
8 document.write(arr)
9 </script>

Program Run Result:

View Source print?
1 Apple,Banana,Orange
2 4
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 vararr = ["Aplle","Banana","Orange","Pear"];
3 vararr2 =  [1,2,3,4];
4 arr.push("NowaMagic");
5 varpopped = arr2.pop();
6 </script>

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.