Implementing Factorial with Recursion

Introduction

Factorial is the traditional first recursive function for a reason: its mathematical definition is already recursive. We write n! = n * (n-1)! for n > 1, and we declare 1! = 1 (and 0! = 1) as the stopping point. That definition hands you both pieces of a recursive function — a base case that returns an answer directly, and a recursive case that reduces the problem to a smaller version of itself.

In this topic you will translate that definition into JavaScript line by line: check for the base case first, then return n * factorial(n - 1). You will then trace factorial(4) by hand, watching stack frames pile up as each call waits on the one below it, and watching the multiplications resolve on the way back out. This tracing habit is the single most valuable skill in the module — it is how you will later reason about Fibonacci, list traversal, and stack overflow risk.

The videos below use Python rather than JavaScript. That is intentional: recursion is a control-flow idea, not a language feature, and the base case / recursive call / call stack structure is identical. As you watch, focus on where the base case is tested, what the recursive call returns, and how the presenter describes the calls unwinding. Then rewrite each example in JavaScript yourself — the syntax change is small, and doing the translation is good practice for MO2.

Video 1: Simple Explanation of Recursion | Recursion Python| Data Structures & Algorithms Tutorial Python #20

codebasics · 14:51 · 139,936 views · 5y ago · captions — Watch on YouTube

About this video

A beginner-oriented introduction to recursion that, as the description promises, aims for an explanation simple enough for a high school student. Expect a walkthrough of what it means for a function to call itself, why a stopping condition is required, and worked code examples in Python (factorial and Fibonacci are the usual companions in this tutorial series).

Why this video

codebasics is a well-known data structures and algorithms tutorial channel, and this entry sits inside an ordered DSA series, so the treatment of base case versus recursive case is deliberate and paced for first-time learners.

Video 2: Recursion Simply Explained with Code Examples - Python for Beginners

Python Simplified · 9:07 · 251,293 views · 4y ago · captions — Watch on YouTube

About this video

A compact explanation of recursion with code examples, framed around the divide-and-conquer idea of breaking a problem into smaller instances of itself. The description also notes a comparison of speed, so expect some discussion of how recursive solutions stack up against iterative ones.

Why this video

The short runtime makes it a good second pass on the same ideas, and the recursion-versus-iteration performance angle previews the trade-off reasoning you will need later in the module.

Video 3: L77: Recursion - an illustration | sum of n numbers, factorial & more

IIT Madras - B.S. Degree Programme · 15:24 · 35,997 views · 5y ago · captions — Watch on YouTube

About this video

A lecture-style illustration of recursion that works through concrete examples, including the sum of n numbers and factorial. Because it builds from sum before reaching factorial, you should see the same structural pattern — base case, then a call on a smaller input — applied more than once.

Why this video

This is formal university coursework from the IIT Madras B.S. Degree Programme, and it is the closest match to this topic because factorial is named explicitly and presented alongside a sibling example that makes the mathematical structure to code structure connection obvious.

How These Videos Support the Learning Outcomes

Together these videos build the foundation the module outcomes rest on. All three force you to separate the base case from the recursive case and explain why the stopping condition prevents runaway recursion (MO1), and their worked factorial code gives you a template to reimplement in JavaScript and later extend to Fibonacci and list traversal (MO2). The step-by-step walkthroughs of calls being made and returned support hand-tracing stack frames for something like factorial(3) (MO3), while the recursion-versus-iteration and speed discussion in the Python Simplified video opens the evaluation and performance questions addressed by MO4 and MO5.

About These Videos

These videos were chosen from YouTube on October 2, 2026. Each has captions; the player turns them on. Videos on YouTube can be changed or removed — if one no longer plays, tell your instructor. If a player does not appear, use its Watch on YouTube link.