splice() changes an array at a chosen index. It can remove elements, insert new elements, or replace existing elements, and it returns an array containing the elements that were removed.
const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
const removed = scripts.splice(2, 2, "VBScript", "Perl");
console.log(scripts); // ["PHP", "ASP", "VBScript", "Perl"]
console.log(removed); // ["JavaScript", "HTML"]
array.splice(start)
array.splice(start, deleteCount)
array.splice(start, deleteCount, item1, item2)
Array indexes and array length determine where positional changes happen. start selects the index to change. deleteCount controls how many existing elements are removed. Any additional arguments are inserted beginning at start.
splice() changes the original array. Use toSpliced() when you need a new array instead.The original page replaced two items beginning at index 2:
const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
scripts.splice(2, 2, "VBScript", "Perl");
console.log(scripts); // ["PHP", "ASP", "VBScript", "Perl"]
Two elements were removed and two new elements were inserted, so the final length stayed the same.
Use a deleteCount of 0 to insert without removing.
const scripts = ["PHP", "ASP"];
scripts.splice(2, 0, "VBScript", "Perl");
console.log(scripts); // ["PHP", "ASP", "VBScript", "Perl"]
The original page demonstrated using array.length as the insertion index:
const scripts = ["PHP", "ASP"];
scripts.splice(scripts.length, 0, "JavaScript", "HTML");
console.log(scripts);
For ordinary appending, push() is usually simpler.
const scripts = ["JavaScript", "HTML"];
scripts.splice(0, 0, "PHP", "ASP");
console.log(scripts); // ["PHP", "ASP", "JavaScript", "HTML"]
For ordinary insertion at index 0, unshift() directly expresses the intention.
const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
scripts.splice(1, 1);
console.log(scripts); // ["PHP", "JavaScript", "HTML"]
To remove two elements starting at index 1:
const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
scripts.splice(1, 2);
console.log(scripts); // ["PHP", "HTML"]
If you call splice(start) without deleteCount, all elements from start to the end are removed.
const values = [10, 20, 30, 40, 50];
values.splice(2);
console.log(values); // [10, 20]
const values = ["a", "b", "c", "d"];
const removed = values.splice(1, 2);
console.log(removed); // ["b", "c"]
console.log(values); // ["a", "d"]
When nothing is removed, the returned array is empty.
A negative start counts from the end of the array.
const values = ["a", "b", "c", "d"];
values.splice(-2, 1, "X");
console.log(values); // ["a", "b", "X", "d"]
Combine indexOf() with splice(), but always guard against a -1 result.
const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
const index = scripts.indexOf("ASP");
if (index !== -1) {
scripts.splice(index, 1);
}
console.log(scripts); // ["PHP", "JavaScript", "HTML"]
If the goal is to remove every element matching a condition and you do not need to mutate the original array, filter() is often clearer.
const values = [1, 2, 2, 3, 2, 4];
const withoutTwos = values.filter((value) => value !== 2);
console.log(values); // [1, 2, 2, 3, 2, 4]
console.log(withoutTwos); // [1, 3, 4]
const original = ["a", "b", "c", "d"];
const updated = original.toSpliced(1, 2, "X", "Y");
console.log(original); // ["a", "b", "c", "d"]
console.log(updated); // ["a", "X", "Y", "d"]
toSpliced() follows the same general positional idea but returns a new array rather than modifying the source.
| Method | Changes original? | Purpose |
|---|---|---|
splice() | Yes | Add, remove, or replace at a position |
slice() | No | Copy a selected range |
toSpliced() | No | Create an updated copy with positional changes |
splice() with slice().splice() mutates the original array.deleteCount = 0 when inserting without removing.indexOf() result of -1 directly to splice().splice() when a clearer method such as push(), unshift(), or non-mutating filter() better expresses the task.Empty an Array Array Reference Display Array Elements
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.