Skip to content
Queue in JavaScript

Implementing Queue in JavaScript

A Queue is a data structure that follows the FIFO (First In, First Out) principle. Think of it like a real-world queue 🧑‍🤝‍🧑 where people join from the last and exit from the start.

Operations performed on a Queue

  • isEmpty – Checks if the queue is empty.
  • peek – Returns the value at the front of the queue without removing it.
  • enqueue – Adds a new value to the back of the queue.
  • dequeue – Removes and returns the value at the front of the queue.

JavaScript Code for a Queue

I have implemented the JavaScript code below for a queue data structure using classes and nodes. In a way, it’s almost similar to the stack code.

Note: Each node stores its own data and a pointer (or reference) to the next node location (memory address).

class Node {
    constructor(value) {
        this.value = value; // Data stored in the node
        this.next = null; // Pointer to the next node
    }
}

class Queue {
    constructor() {
        this.first = null;
        this.last = null;
        this.size = 0;
    }

    // Checks if the queue is empty
    isEmpty() {
        return this.size === 0;
    }

    // Returns the value of the head node
    peek() {
        return this.first?.value;
    }

    // Adds a new node with the given value to the end of the queue
    enqueue(value) {
        const newNode = new Node(value);
        if (this.isEmpty()) {
            // If the queue is empty, the new node becomes both the first and last
            this.first = newNode;
            this.last = newNode;
        } else {
            // Otherwise, add the new node to the end and update the 'last' pointer
            this.last.next = newNode;
            this.last = newNode;
        }
        return ++this.size;
    }

    // Removes the first node from the queue and returns it's value
    dequeue() {
        if (this.isEmpty()) return undefined;
        const temp = this.first; // Save reference to the current first node
        this.first = this.first.next; // Update 'first' to be the next node in queue
        this.size--;
        // If the queue is now empty after dequeueing, update 'last' to be null
        if (this.isEmpty()) {
            this.last = null;
        }
        return temp.value;
    }
}

const myQueue = new Queue();

console.log(myQueue.enqueue("value 1")); // 1
console.log(myQueue.enqueue("value 2")); // 2
console.log(myQueue.enqueue("value 3")); // 3
console.log(myQueue.first)
/* {
  "value": "value 1",
    "next": {
    "value": "value 2",
      "next": {
      "value": "value 3",
        "next": null
    }
  }
} 
*/
console.log(myQueue.size) // 3
console.log(myQueue.isEmpty()); // false
console.log(myQueue.peek()); // value 1
console.log(myQueue.last) // Node { value: 'value 3', next: null }
console.log(myQueue.dequeue()); // value 1

Time Complexity – O(1)

All the operations (enqueue, dequeue, peek, and isEmpty) take constant time O(1).

Real World Applications of a Queue

  • First come, first serve logic is also based on a queue data structure
  • Customer service lines are in a queue order for ticket booking apps

Data Structures in JavaScript Series

Back to Top