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.
See some of the common usage as below
let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
array.slice() // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
array.slice(0, 2) // [0, 1]
array.slice(1, 4) // [1, 2, 3]But I never knew the slice method takes negative integers for the start and end index. It’s a great way of getting the last items of an array.
let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
array.slice(-1) // [9]
array.slice(-2, -1) // [8]
array.slice(-3, -1) // [7, 8]
array.slice(-1, -3) // [] Because -1 > -3, so it is like you do array.slice(3, 1) where start is greater than endWhere else than MDN 🤣