Skip to content

Stack Overflow

Medium — good to knowGeneral Dev

ELI5 — The Vibe Check

A stack overflow happens when a function keeps calling itself forever, like a mirror facing a mirror. Each call gets stacked on top of the last, and eventually the stack runs out of room and crashes. It is also the name of the website every developer has open constantly.

Real Talk

A stack overflow is a runtime error caused by excessive use of the call stack, most commonly from infinite or deeply nested recursion. Each function call pushes a stack frame onto the call stack, which has a fixed maximum size. When that limit is exceeded, the program crashes with a 'Stack Overflow' or 'Maximum call stack size exceeded' error.

Show Me The Code

// Infinite recursion = stack overflow:
function countdown(n) {
  console.log(n);
  countdown(n - 1); // forgot the base case!
}
countdown(10);
// RangeError: Maximum call stack size exceeded

// Fix: add a base case
function countdown(n) {
  if (n <= 0) return;
  console.log(n);
  countdown(n - 1);
}

When You'll Hear This

"You forgot the base case, that's why it stack overflowed." / "Stack Overflow has an answer for this for sure."

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