Removing the first element of a JavaScript array. Well, the shift method provides a very simple way of removing the first element of a JavaScript array. So back to our foo array. This time the array contains the letters A, B, and C. So what if I wanted to remove a from the rear of the array, I want to remove the first element of the array? Well, I could call the shift method. And when I call the shift method, what happens is a is removed from the array.
But then I wind up with a letter a kind of floating around there. And my next question is, well, what is this? Why don't I get the letter A there. And that's because the shift method always returns the element that was removed from the array. So if I were to call the shift method one more time here, it would remove the letter B, and return it because the letter B was the first element in the array. It was the first element and now I call shift again, and it removes B from the array, but it returns the letter B, it returns the element that was removed from the array.
So let's look at one more code example for this one. in JavaScript, the shift method removes the first element of an array and returns that element. Here I have an array with six elements, the first element is the letter A. So if I call the shift method on that array, it should remove the first element, which is a letter A and return it. Let's give it a try. So first, I'll define this array and I now have an array called foo, that has six elements and the first element is the letter A.
So now if I call the shift method on this array, it should remove the first element which is the letter A, and it should return that element. So return the letter A. And if I examine the array, we can see that it did remove the first element which is the letter A. So if I call the shift method again, it should remove returns a letter B. And I can see that if I examine foo, the letter B is missing and the letter C is now the first element is only for L. In the array, so if I call shift one more time, it removed the first element, which is a letter C. And if I examine foo, I can see it only has three elements. Because the first element of C was removed, the first element is D. So in JavaScript, the shift method, always returns removes the first element of an array and it returns the element that it removed.