How do you get just the first n items of an array? Just use the built-in slice() method that comes with each array instance

How do you get just the first n items of an array?

Just use the built-in slice method that comes with the array instance

This method returns a shallow copy of a portion of an array into a new array selected from start to end (end is not included)

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const n = 5 //get the first 5 items

const newArr = arr.slice(0, n);

console.log(newArr);
// expected output: [1, 2, 3, 4, 5]

console.log(arr);
// expected output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Note: the original array is not modified in this operation