Community Post

Data Structure in Web - Queues

What is a Queue?

Queue is a programming construct which bears heavy resemblance with the real world queues for example a queue at the movie theater, ATMs or the bank.

A more computer sciency definition of queue would be

An abstract data collection in which the elements can be added to the back called enqueue and removed form the front called dequeue which makes it a FIFO data structure

Table of Contents

    Ofcourse having only enqueue and dequeue operations are not going to be enough for our Queue, so we are going make it similar to the Queue in Java.


    Before we jump in and implement a queue, let us take a look at the different types of queues:

    1. Simple Queue - The queue discussed above

    2. Circular Queue - Similar to simple queue except the back of the queue is followed by the front of the queue

    3. Priority Queue - Contains elements with a predefined priority

    4. Dequeue (Double Ended queue) - Similar to Simple Queue but can add or remove elements for either the front or the back of the queue


    1. add - Push an item to the back of the queue
    2. remove - Remove an item from the start of the queue
    3. peek - Show the last item pushed into the queue
    4. clear - Empty the queue
    5. size - Get the current size of the queue

    Creating a Queue

    Simple Queue

    Simple queue, as the name suggests, is a FIFO data structure in which we can push the data from the back and pop it from the front.

    We are going to skip the pleasantries and go straight to using closures for creating the Queue, details are explained in the previous article here

    var Queue = (() => {
        let items = [];
        class Queue {
            constructor() {
            add(element) {
            remove() {
                return items.shift();
            peek() {
                return items[items.length - 1];
            clear() {
                items = [];
            size() {
                return items.length;
        return Queue;

    which when executed returns

    var queue = new Queue();
    console.log(queue.items); // prints undefined
    console.log(queue.size()); // prints 2
    console.log(queue.remove()); // prints 10
    console.log(queue.size()); // prints 1
    console.log(queue.size()); // prints 0

    Circular Queue

    The difference between a Circular queue and a simple queue is that the back of the queue is followed by the front of the queue.

    That being said, they arent functionally different. They still perform the same operations which produce the same results, then you might be wondering where exactly do they differ and why does it matter if the end result is the same.


    Further Reading

    To read more about Stacks, Queues and other data structures that can be used in JavaScript, please purchase my book which explains crucial data structures and algorithms with real world examples.

    Kashyap Mukkamala

    5 posts

    I'm a Developer Advocate at Egen Solutions Inc, I fiddle around with JavaScript, Angular, Ionic & NativeScript during the day. Devoted Brogrammer.