Imagine that you have a list of restaurants that you want to try out, and after finally getting dinner at one of them, you don’t just want to cross the restaurant off your list, but you want to remove it completely.
Array.splice(start, removeCount, newItem, newItem, newItem, ...)
Going back to the example we brought up at the beginning of this tutorial. Say our restaurant bucket list consisted of A, B, C, and D, this would be the original array. The restaurant that we tried would be D. To remove D, we would need to know its index number. Since the array index starts at 0, the index would be (X-1). In this case, the index number would be 3.
const restaurants = [“A”, “B”, “C”, “D”] console.log(restaurants.splice(3, 1)) // output: [“D”] console.log(restaurants) // output: [“A”, “B”, “C”]
Here, we can see that the original array has been spliced into two: the original array with the remaining elements, and the return value is an array with the removed element(s).
For example, let’s add E to our array.
const restaurants = [“A”, “B”, “C”, “D”] console.log(restaurants.splice(3, 0,”E”)) // output:  console.log(restaurants) // output: [“A”, “B”, “C”, “E”, “D”]
This also works if you’d like to both remove and add elements to your array. For example, let’s remove D from the above array and add F and G in D’s location.
const restaurants = [“A”, “B”, “C”, “D”] console.log(restaurants.splice(3, 0,”E”)) // output: [“D”] console.log(restaurants) // output: ["A", "B", "C", "F", "G"]