# JavaScript Recursion

Learn the basics of Recursion in JavaScript

A function can call itself.

This is what recursion means. And it allows us to solve problems in a neat way.

To do so, you need a named function expression, in other words this:

``````function doSomething() {

}
``````

So we can call `doSomething()` inside `doSomething()`.

The simplest example we can make is calculating a factorial of a number. This is the number that we get by multiplying the number for (number - 1), (number - 2), and so on until we reach the number 1.

The factorial of 4 is (4 * (4 - 1) * (4 - 2) * (4 - 3)) = 4 * 3 * 2 * 1, which is 24.

We can create a recursive function to calculate it automatically:

``````function factorial(n) {
return n >= 1 ? n * factorial(n - 1) : 1
}

factorial(1) //1
factorial(2) //2
factorial(3) //6
factorial(4) //24
``````

We can also use an arrow function if we prefer:

``````const factorial = (n) => {
return n >= 1 ? n * factorial(n - 1) : 1
}

factorial(1) //1
factorial(2) //2
factorial(3) //6
factorial(4) //24
``````

Now it's a good time to talk about the call stack.

Imagine we do an error, and instead of calculating the factorial as

``````const factorial = (n) => {
return n >= 1 ? n * factorial(n - 1) : 1
}
``````

we do this:

``````const factorial = (n) => {
return n >= 1 ? n * factorial(n) : 1
}
``````

As you can see, we are calling `factorial(n)` ad infinitum. There's no end, because we forgot to lower it on every call.

If you run this code, you'll get this error:

``````RangeError: Maximum call stack size exceeded
``````

Every time a function is invoked, JavaScript needs to remember the current context before switching to the new one, so it puts that context on the call stack. As soon as the function returns, JavaScript goes to the call stack and picks the last element that was added, and resumes its execution.

Maximum call stack size exceeded means that too many elements were put on the stack, and your program crashed.

THE VALLEY OF CODE

THE WEB DEVELOPER's MANUAL

You might be interested in those things I do:

• Learn to code in THE VALLEY OF CODE, your your web development manual
• Find a ton of Web Development projects to learn modern tech stacks in practice in THE VALLEY OF CODE PRO
• I wrote 16 books for beginner software developers, DOWNLOAD THEM NOW
• Every year I organize a hands-on cohort course coding BOOTCAMP to teach you how to build a complex, modern Web Application in practice (next edition February-March-April-May 2024)
• Learn how to start a solopreneur business on the Internet with SOLO LAB (next edition in 2024)
• Find me on X