What is recursion in JavaScript?

Recursion is a programming technique in which a function calls itself. This allows the function to repeat itself until it reaches a specific condition.

In JavaScript, recursion can be used to solve problems that can be broken down into smaller, similar subproblems.

Example

For example, the factorial of a number is the product of that number and all the positive integers less than it. The factorial of 5, for example, would be 5 x 4 x 3 x 2 x 1, or 120. This problem can be solved using a recursive function in JavaScript as follows:

function factorial(n) 
{
  if (n === 1) 
  {
    return 1;
  }
  
  return n * factorial(n - 1);
}

In this function, the if statement is the base case that tells the function when to stop calling itself. In this case, the function stops when n is equal to 1 and returns 1. If n is not equal to 1, the function calls itself with n - 1 as the argument, effectively reducing the problem by 1 each time.

For example, if we call the factorial function with the argument 5, the function will return 120 by calling itself in the following sequence:

  • factorial(5) is called.
  • factorial(4) is called.
  • factorial(3) is called.
  • factorial(2) is called.
  • factorial(1) is called and returns 1.
  • factorial(2) returns 2 * 1 = 2.
  • factorial(3) returns 3 * 2 = 6.
  • factorial(4) returns 4 * 6 = 24.
  • factorial(5) returns 5 * 24 = 120.

Mutual recursion

Mutual recursion is a situation in which two or more functions call each other recursively to solve a problem. This is different from regular recursion, in which a single function calls itself.

For example, consider the following two functions:

function isEven(n) 
{
	if (n === 0) 
		return true;

	return isOdd(n - 1);
}

function isOdd(n) 
{
	if (n === 0)
		return false;
		
	return isEven(n - 1);
}

In this example, the isEven function returns true if the given number is even, and false otherwise. It does this by calling the isOdd function with n - 1 as the argument. Similarly, the isOdd function returns true if the given number is odd, and false otherwise, by calling the isEven function with n - 1 as the argument.

This example shows how mutual recursion can be used to solve problems that can be divided into two or more subproblems that are solved in a recursive manner. However, it is important to use mutual recursion carefully, as it can be difficult to understand and debug. It is also important to define the base case correctly to avoid infinite loops.

Conclusion

Recursion can be a powerful tool in JavaScript, but it is important to use it carefully. If the base case is not defined correctly, the function can continue calling itself indefinitely, resulting in an infinite loop.

Tip: Always save your code before running a recursive program. If you have a bug, the browser may freeze due to the infinite loop and you will loose your changes.

It is also important to consider the performance of recursive functions, as they can be slower and use more memory than non-recursive solutions.

More resources

Please visit the Download section of codeguppy.com and download the Recursion PDF to see eight classical problems solved both with and without recursion.

Read more blog articles Browse JavaScript projects

About codeguppy

CodeGuppy is a FREE coding platform for schools and intependent learners. If you don't have yet an account with codeguppy.com, you can start by visiting the registration page and sign-up for a free account. Registered users can access tons of fun projects!


Follow @codeguppy on Twitter for coding tips and news about codeguppy platform. For more information, please feel free to contact us.