JavaScript Array splice() method
changes the contents of an array by removing or replacing existing elements and/or adding new elements

splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements.

syntax

splice(index, count, item1, item2, ..., itemN)

index The index (position) to add or remove elements.

count (optional). Number of elements to be removed from the array from start index.

item1, …, itemN (optional). The new elements(s) to be added.

splice() method returns an array containing the deleted elements.

Negative index counts back from the end of the array

If you do not specify any elements, splice() method will only remove elements from the array.

If only one element is removed, an array of one element is returned.

If count is 0 or negative, no elements are removed.

If no elements are removed, an empty array is returned.

splice(0, 0, ...elements) inserts elements at the start of the array like unshift() method.

To create a new array with elements removed and/or replaced without mutating the original array, use toSpliced() method.

To access part of an array without modifying it, use slice() method.

syntax 1 description
remove all items from selected index position to end of myArray
myArray.splice(index)
syntax 2 description
remove all items from selected index position for specified count
myArray.splice(index, count)
syntax 3 description
remove all items from selected index position for specified count and replace with item1
myArray.splice(index, count, item1)
syntax 4 description
remove all items from selected index position for specified count and replace with item1 and item2
myArray.splice(index, count, item1, item2, ..., itemN)