JS Built-ins
← Array165 / 226
ArrayArray.prototype.shiftintermediate

Array.prototype.shift() β€” dequeue from a queue

πŸ“– Usage Example
const queue = []
queue.push('first')
queue.push('second')
queue.shift()  // β†’ 'first'  (FIFO)

Use push() and shift() together to implement a first-in, first-out (FIFO) queue.

Implementing a queue with shift

A queue is a FIFO structure: elements are added to the back (push) and removed from the front (shift).

const queue = ['first', 'second', 'third']
const served = queue.shift() // 'first'
πŸ’‘ Hints (2)
  • β€’ `shift()` is the array operation for "dequeue" β€” it removes from the front.
  • β€’ The returned value is the element that was at index 0.