Splice is another mehtod on the Array Prototype; it is in many ways
Slice’s weird brother in law. The main difference is that splice mutates
the array you run it on, while slice does not. I personally never use
splice because mutation often lead to unintended circomstances.
The other thing about splice is that you can’t do all of what you can do
with slice; for example the cool -1 tricks for removing stuff at the
end. This is where it get’s a little bit “weird”. You can use it to
remove stuff from an array. It takes a start argument and a number
argument, for example like below (removing the two first elements)
But you can also feed it any number of elements after the number
argument which will be inserted where you removed the elements. Splice
can be very useful, but be careful, because everything is just weird
enough that it is very easy to do something you didn’t want to and cause
some difficult to spot bugs.
Let me end with the key differences between slice and splice:
Slice doesn’t mutate
Slice works with start and end position
Splice works with a start position and a “deleteCount”.
Splice can also append elements.