JavaScript Array.prototype.splice() Method
The splice() method changes an array in place by removing, replacing or inserting elements at a given position.
Description
The splice() method changes the contents of an array in place: it can remove elements, insert new ones, or both at once. It returns an array of whatever was removed.
arr.splice(i, 1) removes one element at index i, and arr.splice(i, 0, x) inserts x there without removing anything. Because it mutates the array, use toSpliced() when you need an unchanged original.
A negative start counts from the end, so arr.splice(-1, 1) removes the last element. If deleteCount is left out, everything from start onwards is removed, which is a quick way to truncate an array. Because it shifts the following elements, using splice() inside a forward loop can skip items; loop backwards when removing several.
Syntax
array.splice(start)
array.splice(start, deleteCount)
array.splice(start, deleteCount, item1, item2, ...)Parameters
- startThe index to start changing the array at. Negative values count from the end.
- deleteCountOptional. How many elements to remove. If left out, everything from
startto the end is removed. - item1, item2, ...Optional. Elements to insert at
start.
Return Value
An array of the removed elements (empty if none were removed).
Example
Browser Support
The following information will show you the current browser support for the Array.prototype.splice() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





