The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
Syntax:
slice() slice(start) slice(start, end)
Parameters
startOptional- Zero-based index at which to start extraction.
A negative index can be used, indicating an offset from the end of the sequence.
slice(-2)extracts the last two elements in the sequence.If
startis undefined,slicestarts from the index0.If
startis greater than the index range of the sequence, an empty array is returned. endOptional- Zero-based index before which to end extraction.
sliceextracts up to but not includingend. For example,slice(1,4)extracts the second element through the fourth element (elements indexed 1, 2, and 3).A negative index can be used, indicating an offset from the end of the sequence.
slice(2,-1)extracts the third element through the second-to-last element in the sequence.If
endis omitted,sliceextracts through the end of the sequence (arr.length).If
endis greater than the length of the sequence,sliceextracts through to the end of the sequence (arr.length).
Return value
A new array containing the extracted elements
let myArray = ['A', 'B', 'C', 'D', 'E'] let newArray = myArray.splice(1, 3) //1 means: Find 2nd value in array. //3 means: Take 2 (=3-1) elements from the start point // newArray is ['B', 'C'] ------------------------------------------------------------- let myArray = ['A', 'B', 'C', 'D', 'E'] let newArray = myArray.splice(2) //2 means: Find 3rd value in array. And take the rest. // newArray is ['C', 'D', 'E'] -------------------------------------------------------------
