JavaScript Array splice()
Examples
At position 2, add 2 elements:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");
Try it Yourself »
At position 2, remove 2 items:
const fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
fruits.splice(2, 2);
Try it Yourself »
Definition and Usage
The splice()
method adds and/or removes array elements.
The splice()
method overwrites the original array.
Syntax
array.splice(index, howmany, item1, ....., itemX)
Parameters
Parameter | Description |
index | Required. The position to add/remove items. Negative value defines the position from the end of the array. |
howmany | Optional. Number of items to be removed. |
item1, ..., itemX | Optional. New elements(s) to be added. |
Return Value
An array containing the removed items (if any). |
More Examples
At position 2, add new items, and remove 1 item:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 1, "Lemon", "Kiwi");
Try it Yourself »
Browser Support
splice()
is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |