Array.prototype.splice()
Changes an array in place by removing, replacing, or adding elements.
Syntax
arr.splice(start, deleteCount, ...items) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
start | number | Yes | Index at which to start changing the array. Negative counts from the end. |
deleteCount | number | No | Number of elements to remove starting at `start`. Defaults to all remaining elements. |
items | any | No | Elements to insert at `start`, in order. |
Returns
Array — An array containing the removed elements (empty if none removed).
Examples
const a = [1, 2, 3, 4];
const removed = a.splice(1, 2, 'x');
console.log(a, removed);
Output
[ 1, 'x', 4 ] [ 2, 3 ]
const a = [1, 2, 3];
a.splice(1, 0, 'a', 'b');
console.log(a);
Output
[ 1, 'a', 'b', 2, 3 ]
Notes
Mutates the original array in place. Use `toSpliced` (ES2023) or `slice` for a
non-mutating alternative. Returns the removed elements, not the new array.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 1.0 |
| firefox | 1.0 |
| safari | 1.0 |
| edge | 12 |
| node | 0.10 |