Recursive Functions in JavaScript
7 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:
- A base case, which is the condition under which the function stops calling itself in order to prevent infinite recursion.
- 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(writtenn!) is the product of all positive integers less than or equal ton.n! = 1 x 2 x ... x nFor example: 3! = 1 x 2 x 3 = 6
Let's consider this script, that uses a while loop to iteratively calculate the factorial of an integer:
function factorial(number) {
let sum = 1;
if (number < 0) {
return null;
}
while (number > 1) {
sum = sum * number;
number = number - 1;
}
return sum;
}
console.log('4! = ', factorial(4));
When executed, it will:
- Define a new function named
factorialthat takes as argument a variable namednumberrepresenting an integer. - Define a new local variable named
sumand initialize it with the integer1, used to store the result of thefactorialfunction. - Check if the variable
numberis inferior to0and terminate the function's execution by returning anullvalue if it evaluates totrue. - Otherwise, start a
whileloop that runs until the variablenumberis inferior to1. - Multiply the current value of the variable
sumby the value of the variablenumber. - Decrease the value of the variable
numberby1. - Repeat the
whileloop from step 5. - Return the value of the variable
sum.
Which will produce this output:
4! = 24
💡 Tip: Here is a visual representation of what happens at each iteration of the
whileloop:sum = 1 x 4 = 4 x 3 = 12 x 2 = 24 x 1
Example
Let's consider this script, that uses a recursive function to calculate the factorial of an integer:
function factorial(number, sum = 1) {
if (number < 0) {
return null;
} else if (number <= 1) {
return sum;
} else {
return factorial(number - 1, number * sum);
}
}
console.log('5! = ' + factorial(5));
When executed, it will:
- Define a new function named
factorialthat takes as argument a variable namednumberrepresenting an integer and a variable namedsumused to store the final result. - Check if the value of the variable
numberis less than0and immediately returnnullif it evaluates totrue. - Otherwise, check if the value of the variable
numberis less than or equal to1and stop the recursion by returning the value of the variablesum. - Otherwise, recursively call the function
factorialby decrementing thenumberby1and passing thenumbermultiplied by thesumas the newsum.
Which will produce this output:
5! = 120
💡 Tip: Here is a visual representation of what happens at each call of the
factorialfunction:factorial(5, 1) factorial(4, 5) factorial(3, 20) factorial(2, 60) factorial(1, 120)
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery