splice() method a combination of the methods that add and remove elements from an array. The
splice() method can remove a number of elements and insert elements into an array. The syntax and the function of the
splice() method is shown below.
arrayname.splice(index, count, elements)
countelements, starting from
- Returns a new array, containing the removed elements
splice() methods above show how the
splice() method can be used to achieve a wide range of actions in the array. The first
splice() method removes 2 elements starting from index 0 and adds the elements "one" and "two" at index 0. This is why the end result is ["one", "two", 3, 4, 5]. The second
splice() methods removes no elements from the array and adds "Jill", "Jim", and "Jacob" to the array at index 0. Therefore, the new array is [Jill, Jim, Jacob, John, Jack]. The last
splice() method removes 2 elements from the array starting at index 0. This is why "Jill" and "Jim" are removed from the array.