Search⌘ K
AI Features

Recursive Functions

Explore recursive functions in JavaScript by understanding how functions can call themselves to solve problems like factorial calculations. This lesson helps you grasp recursion's role as an alternative to loops and its use in divide and conquer algorithms, strengthening your functional programming skills.

We'll cover the following...

What are recursive functions?

A recursive function is one that calls itself! This might sound a bit strange, but it’s perfectly possible to place a self-referential function call inside the body of the function. The function calls itself until a certain condition is met. It’s a useful tool when iterative processes are involved.

A common example is a function that calculates the factorialFactorial is a mathematical operation represented by n! that multiplies all positive integers from 1 to n. of a number:

Javascript (babel-node)
function factorial(n) {
if (n === 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}

This function will return 11 if ...