freecoding.school100% FREE · NO SIGNUP
Closure CavesISSUE #8 of 61

recursion · function calls itself

LambdaVSThe Mutator
Lambda saysRecursion is a function calling itself — with a base case that stops it.

A recursive function solves a problem by calling itself on a smaller piece, until a base case ends the chain. It's natural for trees, nested structures, and divide-and-conquer. Always have a reachable base case or you'll blow the call stack ("Maximum call stack size exceeded").

Power-ups you unlock

The Mutator attacks — common mistakes

Boss battleWrite a recursive factorial with a base case at n <= 1.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>function fact(n){ return n<=1 ? 1 : n*fact(n-1); }
document.getElementById('o').textContent='5! = '+fact(5);</script></body></html>
▶ Open the interactive comic issue
‹ Iife · Self-Invoking PatternsThis · What It Points To ›