# Recursion & the Call Stack

> Learn how a function can call itself to solve complex problems, and how JavaScript uses the Call Stack to keep track of everything.

Subject: JavaScript | Lesson 17 of 63 | 7 min read
Interactive lesson: https://codewithanimation.in/javascript/recursion
Previous: [Higher-Order Functions & Callbacks](https://codewithanimation.in/javascript/higher-order-functions.md) | Next: [Objects: Properties & Methods](https://codewithanimation.in/javascript/objects-fundamentals.md)

## What is Recursion?

Recursion is a programming technique where a function calls *itself* in order to solve a smaller piece of a larger problem.

Think of it like opening nested boxes. You open a box, find another box inside, and open that one, repeating the exact same process until you finally find a box with a prize inside.

## The Base Case

Every recursive function MUST have a 'Base Case'. This is the condition that tells the function to stop calling itself.

Without a base case, the function will call itself infinitely. In JavaScript, this eventually causes a 'Maximum call stack size exceeded' error, famously known as a Stack Overflow.

## The Call Stack

JavaScript is single-threaded. It uses a data structure called the 'Call Stack' to keep track of what function is currently running.

When a function calls itself, it pauses its current execution and pushes a new 'frame' onto the top of the stack.

Once the base case is finally reached, the stack begins to 'unwind'. It pops frames off from top to bottom, resolving each one until the original call is finished.

## Common mistake: Memory limits

Because every recursive call adds a new frame to the Call Stack, recursion uses memory. If you recurse too deeply (e.g., 10,000 times), the browser will kill your script to protect the system's memory.

## Tip: Recursion vs Loops

Anything you can do with recursion, you can also do with a standard `while` or `for` loop. Recursion is often preferred for navigating complex, deeply nested structures like File Systems or the browser's Document Object Model (DOM).

## Try it: The Call Stack Visualizer

Step through a recursive `countdown` function. Watch how the Call Stack grows taller with each recursive call, hits the base case, and then unwinds.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/recursion

## Check yourself

### Question 1

What is a 'Base Case' in recursion?

- A. The condition that stops the function from calling itself.
- B. The first line of the function.
- C. A database connection used by the function.

Correct answer: A

The base case is the essential stopping condition. Without it, the function will recurse infinitely.

- Why not B: Incorrect. The base case doesn't have to be the very first line, though it is usually checked early.
- Why not C: Incorrect. Recursion has nothing to do with databases.

### Question 2

What happens if a recursive function forgets its base case?

- A. It causes a Stack Overflow error.
- B. It returns undefined.
- C. It converts into a standard for-loop automatically.

Correct answer: A

Without a base case, the function infinitely adds frames to the Call Stack until it runs out of memory, throwing a 'Maximum call stack size exceeded' error.

- Why not B: Incorrect. It will never get the chance to return anything; the program will crash first.
- Why not C: Incorrect. JavaScript cannot automatically rewrite your broken recursion into a loop.

### Question 3

In what order does the Call Stack resolve frames once the base case is reached?

- A. Top to bottom (Last In, First Out).
- B. Bottom to top (First In, First Out).
- C. All at the exact same time.

Correct answer: A

A Stack is a LIFO (Last In, First Out) structure. The most recent recursive call (at the top of the stack) must finish before the previous call below it can resume.

- Why not B: Incorrect. That describes a Queue. A Stack resolves in reverse order.
- Why not C: Incorrect. JavaScript is single-threaded; it can only resolve one frame at a time.

## Remember this

- Recursion is a function calling itself to solve a smaller piece of a problem.
- A Base Case is mandatory to stop the recursion.
- Each recursive call pauses execution and adds a new frame to the Call Stack.
- When the base case is hit, the Call Stack unwinds from top to bottom (LIFO order).
