When we are using the pop method, the last element of the array is removed and returned. But if we want to remove the first element instead, we can make use of the shift method. So, this shifting method helps us remove the first element from the array, and it returns the removed element.
Let’s have a look at an example, which demonstrates the use of the shift method.
In the above program, we have an array, and we are using the shift method. Due to this method, the first element would get removed from the array, and it will be the return value from the method. Let’s have a look at the output as well →
As you can see, the value GyaniPandit was at the 0 index position, and hence, it was removed. Also, notice that the method has returned the removed value. You can try using this method for practice so that you can get familiar with this method, and we can make use of this method, as and when required.