JavaScript Queue

Summary: in this tutorial, you will learn about the queue data structure and how to implement a JavaScript queue.

Introduction to the Queue data structure

A queue is an ordered list of elements where an element is inserted at the end of the queue and is removed from the front of the queue.

A queue works based on the first-in, first-out (FIFO) principle, which is different from a stack, which works based on the last-in, first-out (LIFO) principle.

A queue has two main operations:

  • Insert a new element at the end of the queue, which is called enqueue.
  • Remove an element from the front of the queue, which is called dequeue.

The following picture illustrates a queue: