JavaScript (ES2026) reference · Array Methods
JavaScript Array splice() Method
Remove, replace or insert elements at a position, changing the array in place. Returns the removed elements.
JavaScript (ES2026) Since ES3
Syntax
array.splice(start, deleteCount, item1, /* …, */ itemN)
Parameters
| Parameter | Description |
|---|---|
start |
Index to start changing. |
deleteCount optional |
How many elements to remove. |
items optional |
Elements to insert at start. |
Return value
An array of the removed elements.
Examples
const months = ["Jan", "Mar", "Apr"];
months.splice(1, 0, "Feb"); // insert
console.log(months);
Output
[ 'Jan', 'Feb', 'Mar', 'Apr' ]
const nums = [1, 2, 3, 4, 5];
const removed = nums.splice(1, 2); // remove two
console.log(removed, nums);
Output
[ 2, 3 ] [ 1, 4, 5 ]
const colors = ["red", "green", "blue"];
colors.splice(1, 1, "yellow", "purple"); // replace one with two
console.log(colors);
Output
[ 'red', 'yellow', 'purple', 'blue' ]
Version history
| Version | Change |
|---|---|
| JavaScript ES3 | Added |
Related
array.slice()· Return a shallow copy of part of the array, from start up to (not including) end. The original is not changed.array.toSpliced()· Return a new array with elements removed, replaced or inserted at a position, leaving the original unchanged.array.with()· Return a copy of the array with one element replaced, leaving the original unchanged. Negative indexes count from the end.- JavaScript Arrays · tutorial
- JavaScript Array Methods · tutorial