Stack and Queue
A stack is last-in, first-out. A queue is first-in, first-out. In JavaScript both are usually arrays: push/pop for a stack, and push/shift or a linked list / deque for a queue.
Detailed Explanation
These are access patterns, not types. The language does not ship Stack or Queue. You pick a backing structure and only use the operations that preserve the pattern.
A stack pushes onto one end and pops from the same end. Array push and pop are amortised O(1) and stay at the end of a packed array. That is the default stack. DFS, undo history, "currently open blocks in a parser", and the JavaScript call stack itself are stack shaped. unshift/shift also form a stack (the front), but they are O(n) because every element reindexes. Do not do that.
A queue adds at one end and removes from the other. push (enqueue at the end) plus shift (dequeue at the front) is the obvious array queue. shift is O(n). For a handful of items it is fine. For thousands of events in a hot loop it is the wrong backing store.
Faster queues: a circular buffer you write yourself, two stacks (inbox / outbox) so each element is moved a constant number of times, or a doubly linked list. In Node, there is no stdlib deque. A simple two-stack queue is enough for interviews and for most application code:
// enqueue to inStack, dequeue by pouring into outStack then popping
Array also has unshift for enqueue-at-front, which is again O(n). A real deque needs O(1) at both ends; use a linked list or a ring buffer if you need that.
unshift/pop is a queue with the ends flipped (add front, remove back). Same costs as push/shift.
What it costs you: choosing the pretty API over the cheap end of the array. A stack that shifts is a performance bug. A queue that shifts on a 100k list is a performance bug. Also, nothing enforces the pattern: a teammate can splice the middle and the type will not complain. Encapsulate the array if the structure is shared.
Reach for push/pop when the newest item should come out first. Reach for a queue when the oldest work should start first (task lists, BFS, event buffers). Reach for a Set plus a queue when BFS needs both order and "already queued" tests.
Code Examples
const undo = [];
undo.push("type A");
undo.push("type B");
console.log(undo.pop()); // => type B
console.log(undo.pop()); // => type Aconst tasks = [];
tasks.push("email"); // enqueue
tasks.push("invoice");
console.log(tasks.shift()); // => email (dequeue, O(n))
console.log(tasks.shift()); // => invoicefunction createQueue() {
const inbox = [];
const outbox = [];
return {
enqueue(x) {
inbox.push(x);
},
dequeue() {
if (outbox.length === 0) {
while (inbox.length) outbox.push(inbox.pop());
}
return outbox.pop();
},
get size() {
return inbox.length + outbox.length;
},
};
}
const q = createQueue();
q.enqueue(1);
q.enqueue(2);
console.log(q.dequeue()); // => 1
console.log(q.dequeue()); // => 2Common Gotchas
`shift` and `unshift` are O(n)
fine for small arrays, costly as a hot queue. Use two stacks or a linked list for large FIFO workloads.
Nothing types a raw array as a stack
document the operations, or wrap them, so callers do not `splice`.
`pop` on `[]` is `undefined`
distinguish empty from a stored `undefined` with a `length` check.
BFS needs a queue, DFS needs a stack (or recursion)
swapping them changes which nodes you visit first.