JavaScript Array.prototype.shift() Method
The shift() method removes the first element of an array and returns it, moving the other elements down.
Description
The shift() method removes the first element and returns it, shifting all other elements down by one index. Together with push(), it lets you use an array as a queue (first in, first out).
On very large arrays, shift() is slow because every element moves; for big queues, keep an index pointer instead.
An array used as a queue with push() and shift() processes items in the order they arrived, which suits task lists and breadth-first searches. Inside a while (queue.length) loop, shift() gives you the next item to process. To read the first element without removing it, use arr[0] or arr.at(0).
Syntax
array.shift()Return Value
The removed element, or undefined if the array was empty.
Example
Browser Support
The following information will show you the current browser support for the Array.prototype.shift() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





