Introducing the parameter delete_count will remove the mentioned number of elements starting from the given index.
Also, if the elements to be added are mentioned, they will be added to the array right from the starting index position.
Let us take an example of an array containing integers and visualize how the splice() method would work on it.
The syntax of the splice() method is:
The splice() method accepts three parameters:
start_index (required): start_index is an integer that shows the index from where the modification would take place. start_index is a mandatory parameter that begins at the0th index from the beginning of the array when it is specified to be non-negative.
When it is negative, the counting would start from the end of the array starting with -1.
- delete_count (optional): delete_count is an integer that denotes the number of elements to be removed from the array. The removal begins at the start_index and goes up to the number of elements as specified by the parameter, delete_count. If not needed, the value delete_count should be 0 because if delete_count is ignored, all the elements starting from start_index are removed.
Return Type: Array
Note: If no element has been removed, an empty array will be returned.
Examples for Understanding
1. Using splice() Method
In the coming example, we'll remove two elements from index 3 (start_index) and add two elements. The starting point for the new elements to be added will be the start_index itself.
2. Using splice() for Different delete_count Values
If delete_count is less than or equal to 0, then none of the elements will be removed.
In a scenario where delete_count is omitted or made greater than the remaining elements of the array from the start_index to the end, then all the elements will be deleted from start_index to the end, i.e.,
3. Using splice() for Different Start Values
Case 1: When start_index is greater than or equal to the length of the array If the start_index is greater than or equal to the length of the array, no element will be removed, and the new elements (if passed as arguments) will be added to the end of the array.
Case 2: When start_index is less than 0. If the start_index is negative, then the elements would be counted from the end of the array. So, the last element's index would be -1 from the end.
The indexing from the start will be as follows:
Case 3: When the sum of start_index and length of the array is less than 0. If start_index + array.length < 0, which means it's beyond the scope of backward indexing as well. So, the indexing will start from 0 itself.
For example, let start_index be -9 and array.length is only 6, so their addition still yields a negative value (-3). So, the indexing will start from index 0.
In the above example, you can observe that the delete_count was 1, so the element at index 0 was removed, and new elements X, Y, Z were added.
- If only the start_index is passed as a parameter, the splice() method will start removing all the elements from the start_index to the end of the array. The same result occurs in the case when delete_count >= (array.length - start_index).
- If delete_count<=0, then no elements will be removed from the array.
- If start_index >= array.length, none of the elements will be removed, and new elements (if passed as arguments) will be added to the end of the array.
- If start_index<0, the indexing starts from the end of the array with the last element having the index -1.
- If start_index + array.length < 0, the indexing starts from index 0.