JavaScript · July 16, 2021

Create Queue Data Structure Using JavaScript Array Methods

I already have an article about it. Click here to see it.

I am not going to details of it but the code implementation.

Queue is also a linear data structure. It follows the principle of First In First Out, and which is also known as FIFO.

There are mainly two operations in Queue. One is enqueue, and another is dequeue. The first one appends data to the first, and the second one removes data from the last.

We will use two array methods to achieve this operation. unshift() is for adding data to the first position. And pop() is for removing data from the last.

const queue = []
// Push
queue.unshift(1)
queue.unshift(2)
queue.unshift(3)
queue.unshift(4)
console.log(queue) // [ 4, 3, 2, 1 ]
// Pop
queue.pop() // 1 is removed
console.log(queue) // [ 4, 3, 2 ]

This very basic data structure. We have already seen how easily we can create it using JavaScript.