Add a new item to an array:
The result of fruits will be:
More "Try it Yourself" examples below.
The push() method adds new items to the end of an array, and returns the new length.
Note: The new item(s) will be added at the end of the array.
Note: This method changes the length of the array.
Tip: To add items at the beginning of an array, use the unshift() method.
The numbers in the table specify the first browser version that fully supports the method.
Method | |||||
---|---|---|---|---|---|
push() | 1.0 | 5.5 | 1.0 | Yes | Yes |
Parameter | Description |
---|---|
item1, item2, ..., itemX | Required. The item(s) to add to the array |
Return Value: | A Number, representing the new length of the array |
---|---|
JavaScript Version: | 1.2 |
Add more than one item:
The output of the code above will be: