Add new items to the beginning of an array:
The result of fruits will be:
The unshift() method adds new items to the beginning of an array, and returns the new length.
Note: This method changes the length of an array.
Tip: To add new items at the end of an array, use the push() method.
The numbers in the table specify the first browser version that fully supports the method.
Method | |||||
---|---|---|---|---|---|
unshift() | 1.0 | 5.5 | 1.0 | Yes | Yes |
Note: The unshift() method returns undefined in Internet Explorer 8 and earlier versions.
Parameter | Description |
---|---|
item1,item2, ..., itemX | Required. The item(s) to add to the beginning of the array |
Return Value: | A Number, representing the new length of the array |
---|---|
JavaScript Version: | 1.2 |