Array.splice()
Mutates an array by removing and/or inserting elements in-place.
array.splice(start, deleteCount?, ...items)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Mutates an array by removing and/or inserting elements in-place.
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place, which modifies the original array (mutation). This operation has a time complexity of O(n) because shifting elements to fill the gap or accommodate new items requires re-indexing all subsequent elements. If the start index is negative, it indicates an offset from the end of the array. If deleteCount is omitted or is greater than the number of elements left in the array, all elements from the start index to the end of the array are deleted. Importantly, splice() returns a new array containing the deleted elements; if no elements are removed, an empty array is returned. This method is preferred for in-place data manipulation but should be avoided in functional programming patterns that require immutability.
Quick reference
Syntax
array.splice(start, deleteCount?, ...items)
Inputs
Parameters
See it in practice
Examples
Removing items from a specific index
var fruits = ['apple', 'banana', 'cherry', 'date'];
var removed = fruits.splice(1, 2);
console.log(fruits);
console.log(removed);['apple', 'date'] ['banana', 'cherry']
Starting at index 1, it removes 2 elements. The original array is updated to contain the remaining items, and the removed items are returned in a separate array.
Inserting elements without deleting
var numbers = [1, 2, 5];
numbers.splice(2, 0, 3, 4);
console.log(numbers);[1, 2, 3, 4, 5]
By setting deleteCount to 0, you can insert any number of elements at the specified index without removing existing data.
Using negative indices for end-of-array edits
var alphabet = ['a', 'b', 'c', 'd'];
alphabet.splice(-1, 1, 'z');
console.log(alphabet);['a', 'b', 'c', 'z']
A negative start index (-1) targets the last element. This example replaces the last element with 'z'.
Debug faster
Common Errors
LogicError
Cause: Confusing splice() with slice(). Splice mutates the original array, while slice returns a shallow copy.
Fix: If you need to keep the original array intact, use slice() or create a copy using the spread operator before splicing.
var original = [1, 2, 3];
var subset = original.splice(0, 1); // original is now [2, 3]LogicError
Cause: Splicing an array while iterating forward through it. When an element is removed, the remaining elements shift left, causing the loop to skip the next element.
Fix: Iterate through the array backwards when performing in-place removals, or use the filter() method to create a new array.
for (var i = 0; i < arr.length; i++) {
if (arr[i].ready) arr.splice(i, 1); // Error: skips next index
}Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Mutates an array by removing and/or inserting elements in-place.
start: Index to start changing the array. deleteCount: How many items to remove. items: Items to insert at start index.
LogicError: If you need to keep the original array intact, use slice() or create a copy using the spread operator before splicing. LogicError: Iterate through the array backwards when performing in-place removals, or use the filter() method to create a new array.