

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. end: end is the index at which we want to stop the extraction.For eg slice(-2) returns the last two elements.

If the start is undefined then it starts extracting from zero.Īnd if the start is greater than the index range, it returns an empty array.Ī negative index can also be used for it.
