Array.prototype.splice()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thesplice()
method ofArray
instances changes the contents of an array byremoving or replacing existing elements and/or adding new elementsin place.
To create a new array with a segment removed and/or replaced without mutating the original array, usetoSpliced()
. To access part of an array without modifying it, seeslice()
.
Try it
const months = ["Jan", "March", "April", "June"];months.splice(1, 0, "Feb");// Inserts at index 1console.log(months);// Expected output: Array ["Jan", "Feb", "March", "April", "June"]months.splice(4, 1, "May");// Replaces 1 element at index 4console.log(months);// Expected output: Array ["Jan", "Feb", "March", "April", "May"]
Syntax
splice(start)splice(start, deleteCount)splice(start, deleteCount, item1)splice(start, deleteCount, item1, item2)splice(start, deleteCount, item1, item2, /* …, */ itemN)
Parameters
start
Zero-based index at which to start changing the array,converted to an integer.
- Negative index counts back from the end of the array — if
-array.length <= start < 0
,start + array.length
is used. - If
start < -array.length
,0
is used. - If
start >= array.length
, no element will be deleted, but the method will behave as an adding function, adding as many elements as provided. - If
start
is omitted (andsplice()
is called with no arguments), nothing is deleted. This is different from passingundefined
, which is converted to0
.
- Negative index counts back from the end of the array — if
deleteCount
OptionalAn integer indicating the number of elements in the array to remove from
start
.If
deleteCount
is omitted, or if its value is greater than or equal to the number of elements after the position specified bystart
, then all the elements fromstart
to the end of the array will be deleted. However, if you wish to pass anyitemN
parameter, you should passInfinity
asdeleteCount
to delete all elements afterstart
, because an explicitundefined
getsconverted to0
.If
deleteCount
is0
or negative, no elements are removed.In this case, you should specify at least one new element (see below).item1
, …,itemN
OptionalThe elements to add to the array, beginning from
start
.If you do not specify any elements,
splice()
will only remove elements from the array.
Return value
An array containing the deleted elements.
If only one element is removed, an array of one element is returned.
If no elements are removed, an empty array is returned.
Description
Thesplice()
method is amutating method. It may change the content ofthis
. If the specified number of elements to insert differs from the number of elements being removed, the array'slength
will be changed as well. At the same time, it uses[Symbol.species]
to create a new array instance to be returned.
If the deleted portion issparse, the array returned bysplice()
is sparse as well, with those corresponding indices being empty slots.
Thesplice()
method isgeneric. It only expects thethis
value to have alength
property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.
Examples
Remove 0 (zero) elements before index 2, and insert "drum"
const myFish = ["angel", "clown", "mandarin", "sturgeon"];const removed = myFish.splice(2, 0, "drum");// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]// removed is [], no elements removed
Remove 0 (zero) elements before index 2, and insert "drum" and "guitar"
const myFish = ["angel", "clown", "mandarin", "sturgeon"];const removed = myFish.splice(2, 0, "drum", "guitar");// myFish is ["angel", "clown", "drum", "guitar", "mandarin", "sturgeon"]// removed is [], no elements removed
Remove 0 (zero) elements at index 0, and insert "angel"
splice(0, 0, ...elements)
inserts elements at the start of the array likeunshift()
.
const myFish = ["clown", "mandarin", "sturgeon"];const removed = myFish.splice(0, 0, "angel");// myFish is ["angel", "clown", "mandarin", "sturgeon"]// no items removed
Remove 0 (zero) elements at last index, and insert "sturgeon"
splice(array.length, 0, ...elements)
inserts elements at the end of the array likepush()
.
const myFish = ["angel", "clown", "mandarin"];const removed = myFish.splice(myFish.length, 0, "sturgeon");// myFish is ["angel", "clown", "mandarin", "sturgeon"]// no items removed
Remove 1 element at index 3
const myFish = ["angel", "clown", "drum", "mandarin", "sturgeon"];const removed = myFish.splice(3, 1);// myFish is ["angel", "clown", "drum", "sturgeon"]// removed is ["mandarin"]
Remove 1 element at index 2, and insert "trumpet"
const myFish = ["angel", "clown", "drum", "sturgeon"];const removed = myFish.splice(2, 1, "trumpet");// myFish is ["angel", "clown", "trumpet", "sturgeon"]// removed is ["drum"]
Remove 2 elements from index 0, and insert "parrot", "anemone" and "blue"
const myFish = ["angel", "clown", "trumpet", "sturgeon"];const removed = myFish.splice(0, 2, "parrot", "anemone", "blue");// myFish is ["parrot", "anemone", "blue", "trumpet", "sturgeon"]// removed is ["angel", "clown"]
Remove 2 elements, starting from index 2
const myFish = ["parrot", "anemone", "blue", "trumpet", "sturgeon"];const removed = myFish.splice(2, 2);// myFish is ["parrot", "anemone", "sturgeon"]// removed is ["blue", "trumpet"]
Remove 1 element from index -2
const myFish = ["angel", "clown", "mandarin", "sturgeon"];const removed = myFish.splice(-2, 1);// myFish is ["angel", "clown", "sturgeon"]// removed is ["mandarin"]
Remove all elements, starting from index 2
const myFish = ["angel", "clown", "mandarin", "sturgeon"];const removed = myFish.splice(2);// myFish is ["angel", "clown"]// removed is ["mandarin", "sturgeon"]
Using splice() on sparse arrays
Thesplice()
method preserves the array's sparseness.
const arr = [1, , 3, 4, , 6];console.log(arr.splice(1, 2)); // [empty, 3]console.log(arr); // [1, 4, empty, 6]
Calling splice() on non-array objects
Thesplice()
method reads thelength
property ofthis
. It then updates the integer-keyed properties and thelength
property as needed.
const arrayLike = { length: 3, unrelated: "foo", 0: 5, 2: 4,};console.log(Array.prototype.splice.call(arrayLike, 0, 1, 2, 3));// [ 5 ]console.log(arrayLike);// { '0': 2, '1': 3, '3': 4, length: 4, unrelated: 'foo' }
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-array.prototype.splice |