Splice, Dice, and Paradise

KennethLatourII
2 min readAug 25, 2022

Okay, only one of these has to do with Javascript but the name was catchy to me. I do get lost in the sauce though when it comes to array-oriented terms. Splice() is one of the most complicated tools I learned in my first week of Software Bootcamp.

Splice() allows you to take out, and add items to your array. Let’s get some things straight first. An array index starts at 0 this is very important when understanding how to harness the full power of Splice(). Now for the anatomy of Splice().

The first parameter to use is the start value; this is where would you like the Splice() to start in the array. The second is the amount that the Splice() will remove. This does not use the index but just a numerical value counting from the start index. Lastly the optional parameter is if you would like to input any new value into the array. Here is a representation of the code.

Let’s dive into some examples shall we.

An important thing to note is that if placing any new value into the array it will take on the index of the start parameter you pass through the Splice(). It will not go before or after it will be that index.

In the code above we can see that the string of green has taken on the index of 1 in the array. But what happens if we want to remove red and replace it with green?

Here it starts at index 1 which is red removes 1 thus taking the value red out of the array and places green in its place. Let’s do one more using multiple removal and replacements.

As you can see the possibilities are endless. Now you can go Splice() in peace.

--

--