Array.prototype.splice()
splice(start[, deleteCount[, item1, item2, ...]]) Returns:
Array · Added in vES1 · Updated March 13, 2026 · Array Methods array splice mutation CRUD
The splice() method changes an array by removing, replacing, or adding elements in place. It modifies the original array and returns an array containing the deleted elements (or an empty array if nothing was removed).
Syntax
array.splice(start)
array.splice(start, deleteCount)
array.splice(start, deleteCount, item1)
array.splice(start, deleteCount, item1, item2, /* ... */)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
start | number | — | Index where to start changing the array. Negative counts from the end. |
deleteCount | number | Infinity | Number of elements to remove. If omitted, removes all elements from start to the end. |
item1, item2, ... | any | — | Elements to add to the array at the start position. |
Examples
Removing elements
const fruits = ['apple', 'banana', 'cherry', 'date'];
const removed = fruits.splice(1, 2);
console.log(fruits);
// ['apple', 'date']
console.log(removed);
// ['banana', 'cherry']
Adding elements
const colors = ['red', 'blue'];
colors.splice(1, 0, 'green', 'yellow');
console.log(colors);
// ['red', 'green', 'yellow', 'blue']
Replacing elements
const numbers = [1, 2, 3, 4, 5];
const removed = numbers.splice(2, 2, 30, 40);
console.log(numbers);
// [1, 2, 30, 40, 5]
console.log(removed);
// [3, 4]
Using negative index
const items = ['a', 'b', 'c', 'd', 'e'];
items.splice(-2, 1, 'X');
console.log(items);
// ['a', 'b', 'c', 'X', 'e']
Using toSpliced() (Immutable)
The toSpliced() method (ES2023) works identically but returns a new array without modifying the original:
const original = [1, 2, 3, 4, 5];
const modified = original.toSpliced(1, 2, 99);
console.log(original);
// [1, 2, 3, 4, 5] — unchanged
console.log(modified);
// [1, 99, 4, 5]
Common Patterns
Removing first element
const arr = [1, 2, 3];
arr.shift(); // slower, reindexes
// Better:
arr.splice(0, 1);
Removing last element
const arr = [1, 2, 3];
arr.pop(); // faster for just removing
arr.splice(-1, 1); // equivalent but slower
Insert at specific position
function insertAt(array, index, ...items) {
array.splice(index, 0, ...items);
}
const nums = [1, 2, 5];
insertAt(nums, 2, 3, 4);
console.log(nums); // [1, 2, 3, 4, 5]