The slice Method in JavaScript returns a section, or slice, of an array. It takes two parameters:
1. The zero-based array element at which to begin the extraction.
2. The zero-based array element at which to end the extraction. If this second parameter isn’t provided, the returned slice is all the array elements beginning with the element specified in the first parameter.
var aryTest = [1,2,3,4];
document.write(aryTest.slice(2));
The preceding example returns 3,4. Since only the first parameter is specified, all elements after the 3 are returned (remember, JavaScript arrays are zero-based).
var aryTest = [1,2,3,4];
document.write(aryTest.slice(2,3));
The preceding example returns 3. The second parameter specifies where the extractions should and and is non-inclusive, meaning it doesn’t include the element at which the end is specified.
Negative numbers can be passed to the slice method. These indicate that the counting for where the slice should be removed is done from the end of the array.
var aryTest = [1,2,3,4];
document.write(aryTest.slice(-3));
The preceding example returns 2,3,4.