Shift, Unshift, push, pop usage--javascript reference manual

Source: Internet
Author: User

Turn from
Http://www.blogbus.com/kingslay-logs/216353709.html

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.
Example: In this example, we will create an array and delete the first element of the array. Note that this will also change the length of the array:

<script type= "Text/javascript" >varnew Array (3) arr[0] = "George"arr[1] = " John "arr[2] =" Thomas "+" <br/> "+" <br/> ") document.write (arr)</ Script>

Output:
George,john,thomas
George
John,thomas

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.
Example: In this example, we will create an array, add an element to the beginning of the array, and return the new length of the array:

<script type= "Text/javascript" >varnew  Array () arr[0] = "George"arr[1] = " John "arr[2] =" Thomas "+" <br/> ") document.write (Arr.unshift (" William ") +" <BR/ > ") document.write (arr)</script>

Output:
George,john,thomas
4
William,george,john,thomas

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.
Example: In this example, we will create an array and change its length by adding an element:

<script type= "Text/javascript" >varnew Array (3) arr[0] = "George"arr[1] = " John "arr[2] =" Thomas "+" <br/> ") document.write (Arr.push (" James ") +" <br/> " ) document.write (arr)</script>
Output:
George,john,thomas
4
George,john,thomas,james

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.
Example: In this example, we will create an array and then delete the last element of the array. Note that this also alters the extent of the array:

<script type= "Text/javascript" >varnew Array (3) arr[0] = "George"arr[1] = " John "arr[2] =" Thomas "document.write (arr) document.write (" <br/> ") document.write ( Arr.pop ()) document.write ("<br/>") document.write (arr)</script>
Output:
George,john,thomas
Thomas
George,john

Shift, Unshift, push, pop usage--javascript reference manual

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.