Optional
start: numberZero-based index at which to start extraction, converted to an integer.
Optional
end: numberZero-based index at which to end extraction, converted to an integer. slice() extracts up to but not including end.
const lyrics = ["Hello", "Darkness", "my", "old", "friend.", "I've", "come", "to", "talk" ];
arrSlice(lyrics); // [ "Hello", "Darkness", "my", "old", "friend.", "I've", "come", "to", "talk" ]
arrSlice(lyrics, 1, 3); // [ "Darkness", "my" ]
arrSlicw(lyrics, 2); // [ "my", "old", "friend.", "I've", "come", "to", "talk" ]
arrSlice(lyrics, 2, 4); // [ "my", "old" ]
arrSlice(lyrics, 1, 5); // [ "Darkness", "my", "old", "friend." ]
arrSlice(lyrics, -2); // [ "to", "talk" ]
arrSlice(lyrics, 2, -1); // [ "my", "old", "friend.", "I've", "come", "to" ]
The arrSlice() 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.
The
arrSlice()
method is a copying method. It does not alter this but instead returns a shallow copy that contains some of the same elements as the ones from the original array.The
arrSlice()
method preserves empty slots. If the sliced portion is sparse, the returned arra is sparse as well.The
arrSlice()
method is generic. It only expects the this value to have a length property and integer-keyed properties.For both start and end, a negative index can be used to indicate an offset from the end of the array. For example, -2 refers to the second to last element of the array.