Javascript queue

 Javascript queue

In Javascript, a queue is a abstract data structure. New items are added to the end of the queue, and old items are removed from the front.

There are two main operations that we need to learn when working with queue's. The insertion of the element is called as enqueue, and the removal of the element is called dequeue.

Enqueue inserts the element at the end of the queue.

Dequeue removes the element from the front of the queue.

Let's consider an example of movie ticket app where people stand in the queue to understand. The people are put in a queue named queue when the names are stored. This is handled by a simple push(enqueueoperation on arr. They are removed from the queue by using the shift(dqueue) operation. As you already they are arrry methods.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
        var queue = ["Amit""Santosh""Laura""Lana"];
        function nextInLine(arritem) {
            arr.push(item); //Enqueue 
            var removed = arr.shift(); //Dequeue 
            return removed;

console.log("Before: " + JSON.stringify(queue));
console.log("After: " + JSON.stringify(queue));


I won't add a video for this as this is a similar concept which is already covered in arrays

Exmple borrowed from FreeCodeCamp.

Post a Comment