JavaScript Data Structures - Queues

A queue can be thought of as a line at a store. The first customer to enter the line is the first customer to exit the line. Queues are used to order process by inserting and removing elements in that queue.

We can implement a simple queue with JavaScript’s arrays. Lets start by creating a simple function which will be responsible for managing our queue

function Queue() { = [];
  this.enqueue = enqueue;
  this.dequeue = dequeue;
  this.peek = peek;

  function enqueue(d) {;

  function dequeue() {;

  function peek() {

We have a function Queue which is responsible for creating a new object. When we invoke Queue, we must use the new operator like so.

const slides = new Queue();

This empty object that is created was then set to the current object value of the prototypes created in the Queue. We can then use our Queue to add and remove slides

const slides = new Queue();

// [1, 2]
// 2
// []

This is a simple data structure which I have used to create wizards. You can see this in action for a website I made at for MyIDCare