
The splice() method is used to remove or replace existing elements in the array. So in case, the object changes you will be able to see the changes on both the original array and the new array. The new array and the original array will refer to the object. In this case, returns the last 3 elements.Ĭase 5: when the end is greater than the index range then it extracts it till the arr.lengthĬase 6: Here it returns the third element from the second to last element.įor objects, the slice always copies object references into a new array. So all elements will be returned starting from the mentioned index(In this case, index 2)Ĭase 3: when the start is greater than the index range, it returns an empty array.Ĭase 4: when passing negative index. Examples of sliceĬase 1: we are passing both the parameters start and end and the returned array includes indexes 1, 2, and 3.Ĭase 2: we are passing only one parameter, omitting the end. Let us see examples of all these cases to have a better understanding. If the end is greater than the index range then it extracts it till the arr.length. If the end is undefined then it extracts the whole array from the starting index number.


This index is not included in the extraction.

Out of which slice and splice are mostly used and sometimes people get confused between the two. We have multiple methods to work on arrays. As we know, arrays in Javascript are variables that are capable of holding multiple values.
