Shift, Unshift, push, pop usage

Source: Internet
Author: User

Shift () Definition and usage

The shift () method removes the first element from the array and returns the value of the first element.

Syntax: Arrayobject.shift ()
Return value: The value of the original first element of the array.

Description: If the array is empty, then the shift () method will not take any action, returning the undefined value. Note that the method does not create a new array, but instead modifies the original arrayobject directly.

Tips and Comments:
Note: This method will change the length of the array.
Tip: To delete and return the last element of the array, use the Pop () method.

unshift () Definition and usage


The Unshift () method adds one or more elements to the beginning of the array and returns the new length.

Syntax: Arrayobject.unshift (newelement1,newelement2,...., newelementx)

Parameter description:
Newelement1 required. The first element to add to the array.
Newelement2 is optional. Adds a second element to the array.
Newelementx is optional. You can add a number of elements.


Return value: The new length of the arrayobject.

Note: the Unshift () method inserts its parameters into the Arrayobject's head and moves the existing elements sequentially to a higher subscript to allow space. The first parameter of the method becomes the new element 0 of the array, and if there is a second argument, it becomes the new element 1, and so on.

Note that the Unshift () method does not create a new creation, but instead modifies the original array directly.

Hints and Notes
Note: This method will change the length of the array.
Note: the Unshift () method does not work correctly in Internet Explorer!
Tip: To add one or more elements to the tail of an array, use the push () method.

push () definition and usage

The push () method adds one or more elements to the end of the array and returns the new length.

Syntax: Arrayobject.push (newelement1,newelement2,...., newelementx)
Parameter description:
Newelement1 required. The first element to add to the array.
Newelement2 is optional. The second element to add to the array.
Newelementx is optional. You can add multiple elements.
Return value: The new length after the specified value is added to the array.

Description: 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.

Tips and Comments:
Note: This method will change the length of the array.
Tip: To add one or more elements to the beginning of an array, use the Unshift () method.

Pop () Definition and usage


The Pop () method is used to delete and return the last element of the array.


Syntax: Arrayobject.pop ()


Return value: The last element of the Arrayobject.
Description: The Pop () method removes the last element of arrayobject, reducing the length of the array by 1, and returning the value of the element it deleted. If the array is already empty, pop () does not change the array and returns the undefined value.

Shift, Unshift, push, pop usage

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.