Skip to content

Stack

Easy — everyone uses thisGeneral Dev

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."

Made with passive-aggressive love by manoga.digital. Powered by Claude.