slice()` method. The `
slice()` method returns a new array containing a portion of the original array, starting from the specified start index and ending at the specified end index.
const arr = [1, 2, 3, 4, 5];
const newArr = arr.slice(1, arr.length - 1);
console.log(newArr); // Output: [2, 3, 4]
In this example, we first create an array `arr` containing five elements. Then we use the `slice()` method to create a new array `newArr` that starts from the second element (`1`) and ends at the second-to-last element (`arr.length - 1`).
This effectively removes the first and last elements from the `arr` array. Finally, we log the `newArr` array to the console. The output is `[2, 3, 4]`, which is the entire array with the first and last elements removed.