So now we are going to take a look at a few more important functions for arrays in JavaScript. The first one is called splice. And what splice does is that it inserts, deletes or replaces an element with another. And I'm gonna show you that. So here suppose that we have an array called days and Saturday, Monday, Tuesday and Wednesday, and we want to insert Sunday in between Saturday and Monday. What we will do is that we are going to use this splice method to insert Sunday in between Saturday and Monday.
And what I'm going to do is just use the days array and then call the splice function. And then the splice function takes three parameters. The first one is a position in which You want to insert an element. So the position in which I want to insert Sunday is position one, or index one. Instead of Monday, I wanted to be Sunday, they want, I want to shift Monday to take the take the second position or second index, and use day to take the place of Wednesday, and so on and so forth. So you want some kind of shift in this in this array.
So the first parameter is the position in which you want to insert Sunday, which is one. The second parameter is the number of elements that you want to Delete to delete starting from that position, starting from the first position from the first parameter. So the first parameter to index one and the number of parameters that we want to delete. In our case, we don't want to delete anything, because all we are going to do is just delete Sitting Sunday, we are not here deleting anything. Therefore what I'm going to pass is just zero. And the third parameter is the value that you want to add in that position.
So what I'm going to add, what I need to add is just Sunday. And finally, what I'm going to do is just print out the result console dot log, I'm going to pass the day's array. And now if I save and run, what I'm going to get is Saturday, Sunday, Monday, and as you can see, Sunday has been inserted between a Saturday and Monday. And to make things more even more clear here with the splice method, if I change the second parameter to one, what I'm gonna do here is that I'm going to delete one element here what I'm gonna, what I'm, I'm telling this place method that I want to insert Sunday at position one, but I want to delete one position starting from from position one, which is going to be Monday. So here what I'm going to do is that I'm going to delete one element, which is Monday, and I'm going to add Sunday.
So here if a Save and Run, what I'm going to get is that Saturday, Sunday and Monday has been deleted. So keep in mind that the second parameter is the number of elements that you want to delete. So this black splice function is very important if you want to add elements in a specific position, or delete elements in from a specific position or even shifting elements and inserting in between. So it's very, very important function that you might consider using a lot rather than other many functions.