In preparation for exercise seven, we need to talk about the splice method. Part of exercise seven will require this method, which allows you to remove or add to an array. Let's look at the syntax of that method first. So since it is a method of the array object, you would use the dot syntax like you do all other methods. So you have your array dot splice, then the very first number inside the parentheses is the index of the element in the array where you want to make your change. Then the next number is how many elements you want to remove from the array.
That could be zero, which means you would not remove any than another comma, and then you start listing items you want to add to the rate. And those are optional. Those items are awesome. Optional. Because you don't have to add to the array, you could simply remove items from the array. Usually, if someone's adding to an array, they will enter the index, they will enter a zero for how many meaning they're not removing any.
And then they will list the items separated by commas that they want to add to the array. Alright, let's look at a few concrete examples. So here's our array first. It's a fruits array, it contains four fruits. Now let's remove something from it to begin with. So here's the command that would remove something fruits dot splice, we indicate that we want to start at index two.
So bananas, zero. Oranges one apple is two. So we're starting there. We are removing one that will remove Apple so we end up with banana Orange and mango. Okay, let's look at a situation where we would add to the array using the same array. Okay, so the one that wasn't modified with this statement here, but using this array here, we will do fruits dot splice, we start at two again, that's Apple, we remove zero so nothing is removed.
Then we add lemon, comma and Kiwi. Now notice where they show up. Here's the results right here. They show up before apple. And so they show up when you're adding something it gets added prior to the index. So we have banana orange, lemon, kiwi, Apple mango.
All right, let me open the console and do a couple of additional examples. So create an array really quick one we've used in the past We have five numbers in this array. Let's say I wanted to remove 78. It's in the middle of the array, so we can't use some of the methods we've learned in the past, such as pop, so we have to use splice. So array dots, dot splice. Now 78 is 012, it's index two, and we want to remove one.
Now notice what gets returned. Whenever you remove something from an array, it returns that item or items as an array. And it modifies the array you were acting on. So it modifies this array. So let's take a look at that now. So simply enter that and notice 78 is gone 78 was returned as an array.
So if I would have set this equal to variable. If I were to use the assignment operator and put that into a variable, I would have two arrays, one with the 78 in it, and then the the original array with the 78 removed. All right, let's add something to this array. Now. I want to add it at position one. So it's going to come before one which is 56.
I'm not going to remove anything, and I want to add a 78. So one is 56 01. Not going to remove it, we're going to add a 78 and remember it will add it before. Let's go ahead and press return returns an empty array because nothing was removed. Now let's take a look at our existing array. Sure enough, the 78 was added before the 56 so Is the splice method and you'll be using the splice method in this next exercise.
One more thing to be aware of with the splice method. There is also a slice method that you may hear of for the array object. Don't mix that up with splice. splice is great for modifying the array on which you are working and therefore is probably used more frequently. All right, let's get you to exercise seven