Queues are similar to stacks, except the insertion point is different from the removal point.

We add at one end of the queue, and we remove from the other end.

This time, we call it First In, First Out (FIFO).

Like any queue you can think of, for example at the restaurant, disco or when you’re waiting to enter into a concert hall.

Here is a possible implementation of a queue in JavaScript using private class fields, using an array as the internal storage:

class Queue {
  #items = []
  enqueue = (item) => this.#items.splice(0, 0, item)
  dequeue = () => this.#items.pop()
  isempty = () => this.#items.length === 0
  empty = () => (this.#items.length = 0)
  size = () => this.#items.length
}

#javascript #programming #web-development #developer

The Queue JavaScript Data Structure
3.35 GEEK