Stack
ELI5 — The Vibe Check
A stack is a pile of things where you can only add to the top and take from the top — like a stack of plates. The last thing you put in is the first thing you take out. This is called LIFO. Your browser's back button is a stack: each page you visit gets pushed, and back pops the top one.
Real Talk
A stack is a LIFO (Last In, First Out) abstract data structure supporting two primary operations: push (add to top) and pop (remove from top). It also supports peek (inspect top without removing). Stacks are used in the call stack for function execution, undo/redo systems, expression parsing, and depth-first search algorithms.
Show Me The Code
// Using an array as a stack in JavaScript:
const stack = [];
stack.push('page1'); // [page1]
stack.push('page2'); // [page1, page2]
stack.push('page3'); // [page1, page2, page3]
stack.pop(); // removes 'page3' (LIFO)
console.log(stack); // [page1, page2]
// Practical: undo history
const history = [];
history.push(action); // do action
const last = history.pop(); // undo
When You'll Hear This
"Use a stack to track the navigation history." / "The call stack is literally a stack data structure."
Related Terms
Data Structure
A data structure is a way of organizing information in your code so it is easy to use and fast to access.
Queue
A queue is like a line at a coffee shop — first come, first served. The first person to get in line is the first to get their coffee.
Recursion
Recursion is when a function calls itself to solve a smaller version of the same problem, like a set of Russian nesting dolls.