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