Javascript Array object unshift () method, arrayunshift

Source: Internet
Author: User

Javascript Array object unshift () method, arrayunshift

The unshift () method can add one or more elements to the beginning of an array and return a new length.

ArrayObject. unshift (newelement1, newelement2,..., newelementX)
Newelement1 is required. The first element added to the array.
Newelement2 is optional. The second element added to the array.
NewelementX is optional. You can add several elements.
The unshift () method inserts its parameters into the arrayObject header, and sequentially moves existing elements to a higher subscript to leave space.
The first parameter of this method will be the new element 0 of the array. If there is a second parameter, it will become the new element 1, and so on.
The unshift () method directly modifies the original array instead of creating a new one. This method changes the length of the array. Method to return the new length of the arrayObject.
Note: The unshift () method cannot work correctly in Internet Explorer!

Tip: to add one or more elements to the end of the array, use the push () method.

Instance
Create an array, add an element to the beginning of the array, and return the new length of the array:

<Script type = "text/javascript"> var arr = new Array () arr [0] = "George" arr [1] = "John" arr [2] = "Thomas" document. write (arr + "<br/>") document. write (arr. unshift ("William") + "<br/>") document. write (arr) </script>

Output:

George, John, Thomas
4
William, George, John, Thomas

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.