惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
D
Docker
腾讯CDC
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
IT之家
IT之家
WordPress大学
WordPress大学
M
MIT News - Artificial intelligence
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
Vercel News
Vercel News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
N
Netflix TechBlog - Medium
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
博客园 - 【当耐特】

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Recursion
Monicah Ajes · 2026-04-27 · via DEV Community
Cover image for Recursion

Monicah Ajeso

"A function that calls itself sounds like a bug. Turns out it's one of the most powerful ideas in programming."

When you first hear "a function that calls itself" — your brain says wait, wouldn't that just loop forever? Good instinct. That's exactly the right question to ask. And the answer is what makes recursion beautiful.

What is Recursion?

Recursion is when a function calls itself to solve a smaller version of the same problem — until it hits a point where it doesn't need to anymore.

Every recursive function has two parts:

  • Base case — the condition that stops the recursion. Without this, you get infinite calls and a crashed browser.
  • Recursive case — where the function calls itself with a smaller input, working towards the base case.

💡 Think of it like: Russian nesting dolls. You keep opening a doll to find a smaller one inside — until you reach the tiniest one that doesn't open. That's your base case.


The Call Stack — A Quick Mention

When a function calls itself, JavaScript doesn't forget what it was doing. Each call gets stacked on top of the previous one — waiting for the one above it to finish first.

countdown(3)
  countdown(2)
    countdown(1)
      countdown(0) ← base case hit, start returning
    ↩ back to countdown(1)
  ↩ back to countdown(2)
↩ back to countdown(3)

Enter fullscreen mode Exit fullscreen mode

This stack of waiting calls is called the call stack. Once the base case is reached, the stack unwinds — each call finishes and returns, one by one.


Example 1 — Countdown

Let's start simple. Count down from any number to zero.

Without recursion:

function countdown(n) {
  for (let i = n; i >= 0; i--) {
    console.log(i);
  }
}

countdown(3); // 3, 2, 1, 0

Enter fullscreen mode Exit fullscreen mode

With recursion:

function countdown(n) {
  if (n < 0) return; // base case — stop here

  console.log(n);
  countdown(n - 1); // recursive case — call with a smaller number
}

countdown(3); // 3, 2, 1, 0

Enter fullscreen mode Exit fullscreen mode

Here's what happens step by step:

countdown(3) → prints 3, calls countdown(2)
countdown(2) → prints 2, calls countdown(1)
countdown(1) → prints 1, calls countdown(0)
countdown(0) → prints 0, calls countdown(-1)
countdown(-1) → n < 0, returns. Done.

Enter fullscreen mode Exit fullscreen mode

💡 The base case here is n < 0 — once we go below zero, stop. Without it, this runs forever.


Example 2 — Factorial

The factorial of a number is that number multiplied by every positive integer below it.

5! = 5 × 4 × 3 × 2 × 1 = 120
4! = 4 × 3 × 2 × 1 = 24
1! = 1
0! = 1 (by definition)

Enter fullscreen mode Exit fullscreen mode

Factorial is a perfect recursion problem because each step is just: "this number times the factorial of the number below it."

Without recursion:

function factorial(n) {
  let result = 1;
  for (let i = n; i > 1; i--) {
    result *= i;
  }
  return result;
}

factorial(5); // 120

Enter fullscreen mode Exit fullscreen mode

With recursion:

function factorial(n) {
  if (n <= 1) return 1; // base case

  return n * factorial(n - 1); // recursive case
}

factorial(5); // 120

Enter fullscreen mode Exit fullscreen mode

Here's what that looks like unwinding:

factorial(5)
= 5 * factorial(4)
= 5 * 4 * factorial(3)
= 5 * 4 * 3 * factorial(2)
= 5 * 4 * 3 * 2 * factorial(1)
= 5 * 4 * 3 * 2 * 1
= 120

Enter fullscreen mode Exit fullscreen mode

💡 The base case is n <= 1 — because 1! = 1 and 0! = 1. Once we hit it, the stack starts returning values back up the chain.


Recursion vs Iteration — Which Should You Use?

Both can solve the same problems. Here's how to think about it:

Recursion Iteration (loops)
Readability Clean for complex problems Clean for simple problems
Performance Can be slower (call stack overhead) Usually faster
Best for Trees, nested structures, divide & conquer Counting, simple repetition

💡 If you can solve it easily with a loop — use a loop. Reach for recursion when the problem naturally breaks into smaller versions of itself.


The Two Rules of Recursion

Before you write any recursive function, ask yourself:

  1. What is my base case? — When do I stop?
  2. Am I moving towards it? — Is each recursive call working towards the base case?

If you can't answer both questions, you're not ready to write the function yet.


Recursion isn't magic — it's just a function with a very good memory and a clear exit plan.

And now you know when to use it.

Happy coding!!! 😊