Skip to content

Queue

In one line

A queue is a FIFO (First-In-First-Out) data structure: the first item added is the first one removed.

In simple words

A queue is a collection where items are added at one end (the back) and removed from the other end (the front). The first item you enqueue is the first one you dequeue — First-In-First-Out (FIFO). Think of a line at a store: people join at the back and are served from the front.

Queues model any situation where fairness or order matters: print job scheduling, message queues between processes, breadth-first search, keyboard input buffers, and task queues in event loops. The core operations are enqueue (add to back) and dequeue (remove from front), both ideally O(1).

A naive array-based queue (using shift() to dequeue) is O(n) because all elements shift. An efficient implementation uses a linked list or a circular buffer. Variants include priority queues (higher-priority items dequeue first) and deques (double-ended queues).

Example

javascript
class Queue {
  #items = [];
  enqueue(item) { this.#items.push(item); }
  dequeue() { return this.#items.shift(); }
  get size() { return this.#items.length; }
}

const tasks = new Queue();
tasks.enqueue("task1"); tasks.enqueue("task2"); tasks.enqueue("task3");
console.log(tasks.dequeue()); // "task1" — first in, first out
console.log(tasks.dequeue()); // "task2"

enqueue adds to the back, dequeue removes from the front. The first task added is the first one processed.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary