Queues

All about queues in Javascript


Queues are used to store data in a "first-in, first-out" (FIFO) order. That is, The first element added is the first one to be removed.

We will use the following methods to implement a queue:

  • push: adds an element at the end of the queue
  • shift: removes the first element from the queue

LinkIconEnqueue push (push at the end)

Adds an element in-place at the end

const arr = [1,2,3,4]
arr.push(5);
arr // -> [1,2,3,4,5]

LinkIconDequeue shift (remove from the front)

const arr = [1,2,3,4]
const head = arr.shift();
arr // -> [2,3,4,5]