So suppose now that you want to delete an element from an array, there are many ways to delete elements from an array. The first one that I'm going to show you is that if you want to delete the first element from an array, to delete the first element from an array, what you would do is that you are going to use a function called shift. So I'm going to use foods, the shift. And then I'm going to use the console log to print the new array console dot log, and I'm going to print this foods array. And what I'm going to get and what I'm going to get is orange fish, coffee and Apple is gone. And as you can see here, if I print out this, if I store this value, and they call it a equals the foods that shift, and I print a what I'm going to get is Apple, Apple has been removed from the foods array and has been added to the this variable called a.
So let's now instead of using shift, I'm going to use pop. And I'm going to print the foods array. Now if I save and run, there you go, we have apple, orange, fish, and coffee gone. And if I print a, I should get coffee and that is true I get coffee. So coffee has been deleted from foods array and has been added to the variable called a. Now suppose that you want to delete a specific element from an array, not as maybe not at the beginning and not at the end.
So suppose that you want to delete this element fish. What you would do is to is to use a function called splice and splice function isn't like this slice and displace function is going to Delete to delete An element. But the problem is that it is first going to take two parameters. The first parameter is the element that you want to start deleting elements from an array. So for example, I want to start deleting elements from index number two, so I will type two. But if I want to delete only one element, which is fish, I should add the second parameter as one.
So this is going this, this format is going to delete only fish. But if I add two, what I'm going to delete is that I'm going to delete two elements starting from position two from index two. So I'm going to delete this one, as well as this one. So here I'm going to delete two elements. So this is how you can delete a specific element. So now suppose that you want to delete orange, what you would do is first you add the element the index of that position Which is one, and then the number of items that you want to start, you want to delete on what from that position on what.
So this position is one. So how many elements you want to delete from this position. If you want only to delete orange, you will type here in the second parameter as one, if you want to delete two elements, orange as well as fish you two. So if I want to delete only orange, I'm going to type one one, and save and run. And I'm going to get apple and then fish and then coffee. So orange has been gone, because here I told the splice function that I want you to leave element, I want to start deleting elements from position one.
And the element the number of elements that I want to delete onward from that position is one which which means that only one position which is orange. So this is how you delete a specific cific element from an array. But if I add to here instead of one and Save and Run, I am going to I'm going to get Apple coffee. So here it deleted orange as well as fish because I told the splice function that I want to do to delete two elements, starting from position one, so I want to delete orange and then fish