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.
myArray.splice(index)
myArray.splice(index, count)
myArray.splice(index, count, item1)
myArray.splice(index, count, item1, item2, ..., itemN)