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