JavaScript Recursive Function by Examples
JavaScript Recursive Function by Examples
Donate Now
(https://www.javascripttutorial.net/donation/)
Suppose that you have a function called recurse() . The recurse() is a recursive function if it calls
itself inside its body, like this:
function recurse() {
// ...
recurse();
// ...
https://www.javascripttutorial.net/javascript-recursive-function/ 1/6
29/07/2022, 08:06 JavaScript Recursive Function By Examples
A recursive function always has a condition to stop calling itself. Otherwise, it will call itself indefinitely.
So a recursive function typically looks like the following:
function recurse() {
if(condition) {
//...
} else {
recurse();
Generally, you use recursive functions to break down a big problem into smaller ones. Typically, you
will find the recursive functions in data structures like binary trees and graphs and algorithms such as
binary search and quicksort.
Suppose that you need to develop a function that counts down from a specified number to 1. For
example, to count down from 3 to 1:
function countDown(fromNumber) {
console.log(fromNumber);
https://www.javascripttutorial.net/javascript-recursive-function/ 2/6
29/07/2022, 08:06 JavaScript Recursive Function By Examples
countDown(3);
function countDown(fromNumber) {
console.log(fromNumber);
countDown(fromNumber-1);
countDown(3);
This countDown(3) will run until the call stack size is exceeded, like this:
The count down will stop when the next number is zero. Therefore, you add an if condition
(https://www.javascripttutorial.net/javascript-if/) as follows:
function countDown(fromNumber) {
console.log(fromNumber);
https://www.javascripttutorial.net/javascript-recursive-function/ 3/6
29/07/2022, 08:06 JavaScript Recursive Function By Examples
if (nextNumber > 0) {
countDown(nextNumber);
countDown(3);
Output:
If the function name is set to null somewhere in the code, the recursive function will stop working.
countDown = null;
newYearCountDown(10);
Error:
https://www.javascripttutorial.net/javascript-recursive-function/ 4/6
29/07/2022, 08:06 JavaScript Recursive Function By Examples
The code causes an error because the body of the countDown() function references the countDown
function name, which was set to null at the time of calling the function.
console.log(fromNumber);
if (nextNumber > 0) {
f(nextNumber);
countDown = null;
newYearCountDown(10);
Suppose you need to calculate the sum of natural numbers from 1 to n using the recursion technique.
To do that, you need to define the sum() recursively as follows:
sum(n) = n + sum(n-1)
sum(n-1) = n - 1 + sum(n-2)
...
sum(1) = 1
https://www.javascripttutorial.net/javascript-recursive-function/ 5/6
29/07/2022, 08:06 JavaScript Recursive Function By Examples
function sum(n) {
if (n <= 1) {
return n;
Summary
A recursive function is a function that calls itself until it doesn’t
A recursive function always has a condition that stops the function from calling itself.
https://www.javascripttutorial.net/javascript-recursive-function/ 6/6