What is the difference between slice and splice?
These two methods look almost the same, but they do completely different things. slice gives you a new array that holds part of the original, and it leaves the original array exactly as it was. splice changes the original array by removing, replacing, or inserting items in place, and it gives you back the items that it removed.
const letters = ['a', 'b', 'c', 'd'];
// slice returns a new array and leaves the original alone.
const part = letters.slice(1, 3);
console.log(part); // [ 'b', 'c' ]
console.log(letters); // [ 'a', 'b', 'c', 'd' ]
console.log(letters.slice(-2)); // [ 'c', 'd' ]
// splice changes the original array.
const removed = letters.splice(1, 2, 'X');
console.log(removed); // [ 'b', 'c' ]
console.log(letters); // [ 'a', 'X', 'd' ]
The two positions that slice takes are a start position and an end position, and the end position is left out. Negative numbers count from the end of the array, so a slice of minus two gives you the last two items. If you leave out the end position, you get everything from the start to the end of the array.
splice takes a start position, a count of items to remove, and then any number of new items to put in that same place. It gives you the removed items as a new array, and it changes the array that it was called on. If you call splice with only a start position, then it removes everything from that position to the end, which is a common way to cut an array short in place.
The similar names are the only real danger here, and they have caught everybody at least once. If you remember that slice gives you a slice back and splice splices things out, you will not get it wrong twice.