Whenever you use shift() on an array, the method does the following:
- It removes its calling array’s first item.
- It returns the removed item.
Syntax of the shift()
Method
shift()
accepts no argument. Here is the syntax:
Example 1: Remove an Array’s First Item
Try it on StackBlitz
You can see that shift()
removed and returned numbersArray
’s first item (1
).
Also, note that shift()
changed the original array’s length.
Example 2: Remove an Array’s First Item
Try it on StackBlitz
The snippet above used shift()
to shift away fruitsArray
’s first item.