JavaScript Array Object Unshift () method

Source: Internet
Author: User
Tags javascript array

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

Arrayobject.unshift (Newelement1,newelement2,...., newelementx)
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.
The Unshift () method inserts its arguments into the Arrayobject's head and moves the existing elements sequentially to a higher subscript so that space can be set aside.
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.
The Unshift () method does not create a new creation, but modifies the original array directly, and the method changes the length of the array. method returns the new length of the arrayobject.
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.

Instance
Creates an array, adds an element to the beginning of the array, and returns the new length of the array:

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

Output:

George,john,thomas
4
William,george,john,thomas

JavaScript Array Object Unshift () method

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.