slice method on arrays takes part of the array and creates a new array from it. Here's an example of using it:
const names = ['Preston', 'Amanda', 'Joe', 'Brooke']; const middle = names.slice(1, 3); console.log(middle); // ['Amanda', 'Joe']
slice method takes at least one parameter, but a second is optional. The required parameter is the index that you want to start the slice at. In our example above, we start at index 1, which is the string "Amanda". The second parameter is optional. If you provide it, it's the end index. However,
slice ends at but does not include that last index. The item at index 3 in the above array is the string "Brooke". So even though the 3 is passed in to the
slice method, "Brooke" is not included. If you don't provide a second parameter to the method, then everything from the start index to the end of the array is returned.
One more thing to note is that the
slice method does not alter the original array.