Free Tool

Recursion Visualizer

Recursion is when a function solves a problem by calling itself on a smaller version of the same problem until it reaches a base case it can answer directly. Each call waits on the call stack until the one above it returns.

Step through the examples below one call and return at a time, break the base case to see a stack overflow, then fix it. Everything runs in your browser.

Interactive visualizer

Step through the call stack

Each step is one call or one return. Watch frames pile up until the base case answers, then unwind as each caller uses the value below it.

Code

  1. function factorial(n) {
  2. if (n <= 1) return 1;
  3. return n * factorial(n - 1);
  4. }

Call stack (1 frame, top first)

  1. factorial(4)running

Step 1 of 8 · Call

Call factorial(4). A new frame with n = 4 goes on top of the stack.

Anatomy

Base case and recursive case

  • Base case: the smallest input, answered without another call. For factorial that is n <= 1.
  • Recursive case: call the same function on a smaller input and combine the result, like n * factorial(n - 1).
  • Progress: every call must move closer to the base case. If it can skip past it, as n < 0 does, the answer is wrong or the calls never stop.

Call stack

What the call stack does

Each call pushes a frame holding its own copy of the arguments and local variables. The newest frame runs; the ones below it wait. When the base case returns, frames pop off one by one and each caller finishes its work with the value it got back. Runtimes cap how deep the stack can grow, so endless recursion ends in a stack overflow such as JavaScript's RangeError: Maximum call stack size exceeded.

Examples

Recursion examples you will meet

Factorial

n! = n × (n − 1)!, with 1! = 1 as the base case. The classic first example.

Fibonacci

fib(n) = fib(n − 1) + fib(n − 2). Two calls per step, so it shows why memoization matters.

Tree traversal

Visit a node, then recurse into each child. The DOM, JSON and file systems are all trees.

Nested data

Flatten or sum lists inside lists of any depth, like the second example above.

Divide and conquer

Merge sort and quicksort split the input in half, sort each half recursively and combine.

Backtracking

Sudoku solvers and maze search try a choice, recurse, and undo it if it fails.

Compare

Recursion vs iteration

AspectRecursionLoop
Reads best forTrees, nested data, divide and conquerCounting, simple sequences, streaming
MemoryOne stack frame per active callConstant extra memory
Stopping ruleBase caseLoop condition
Common bugMissing or unreachable base case (stack overflow)Off-by-one loop bounds (infinite loop)
RewriteAny recursion can become a loop with an explicit stackAny loop can become tail recursion

How to

How to write a recursive function

  1. Step 1

    Pick the smallest input

    Decide which input you can answer directly, such as n <= 1 or an empty list. That is the base case.

  2. Step 2

    Shrink the problem

    Write the recursive case so each call works on a strictly smaller input, such as n - 1 or one nested list.

  3. Step 3

    Combine the results

    Use the value the smaller call returns to build this call's answer, such as n * factorial(n - 1).

  4. Step 4

    Trace a small example

    Step through a tiny input by hand or in the visualizer and check that every path reaches the base case.

Learning to read code with AI?

Code Card turns your Claude Code, Codex, and OpenClaw sessions into a public developer profile with activity graphs, token breakdowns, badges, and a shareable card. Start with npx code-card.

Make your Code Card

FAQ

Recursion questions, answered

What is recursion in programming?

Recursion is when a function solves a problem by calling itself on a smaller piece of the same problem. Every recursive function needs a base case that returns an answer directly and a recursive case that moves toward that base case.

What is a base case?

The base case is the input small enough to answer without another call, such as n <= 1 for factorial or a plain number in a nested list. Without a reachable base case the calls never stop and the program runs out of stack space.

What is the call stack?

The call stack is the list of function calls that have started but not finished. Each call pushes a frame holding its own arguments and local variables, and each return pops that frame and hands its value to the caller below it.

What causes a stack overflow in recursion?

A stack overflow happens when recursion goes deeper than the runtime allows, usually because the base case is missing, never reached, or the input is very large. In JavaScript the error is RangeError: Maximum call stack size exceeded.

Is recursion better than a loop?

Neither is always better. Recursion reads naturally for trees, nested data and divide-and-conquer algorithms. A loop uses constant stack space and is often faster for simple counting. Any recursive function can be rewritten with a loop and an explicit stack.