Add items to the array:
The result of fruits will be:
More "Try it Yourself" examples below.
The splice() method adds/removes items to/from an array, and returns the removed item(s).
Note: This method changes the original array.
The numbers in the table specify the first browser version that fully supports the method.
Method | |||||
---|---|---|---|---|---|
splice() | 1.0 | 5.5 | 1.0 | Yes | Yes |
Parameter | Description |
---|---|
index | Required. An integer that specifies at what position to add/remove items, Use negative values to specify the position from the end of the array |
howmany | Required. The number of items to be removed. If set to 0, no items will be removed |
item1, ..., itemX | Optional. The new item(s) to be added to the array |
Return Value: | A new Array, containing the removed items (if any) |
---|---|
JavaScript Version: | 1.2 |
At position 2, add the new items, and remove 1 item:
The result of fruits will be:
At position 2, remove 2 items:
The result of fruits will be: