Recursive Functions in JavaScript

JavaScript·2 min read·Jan 1, 2026

A recursive function is a function that calls itself in order to solve a problem that can be broken down into smaller problems.

function recursive(parameter, result) {  if (baseCase) {    return result;  } else {    // modify parameter    // modify result    return recursive(parameter, result);  }}

It essentially includes:

  1. A base case, which is the condition under which the function stops calling itself in order to prevent infinite recursion.
  2. A recursive case, which is the condition under which the function calls itself with modified arguments, progressing towards the base case.

Example

📚 Definition: In mathematics, the factorial of a non-negative integer n (written n!) is the product of all positive integers less than or equal to n.