# I don't understand the recursion

Hi. I know how to define and use a function like this:

``````function sum(a, b) {
return a + b;
}
console.log(sum(3, 4)); // returns 7
``````

And I also learned how to use an arrow function like this:

``````let array = [
1,
2,
3,
4
];
let doubled = array.map(i => {
return i * 2;
});
console.log(doubled); // returns 2, 4, 6, 8
``````

But I have a question.

I don’t understand the recursion and how recursion works that kinda look like this:

``````function factorial(number) {
if (number === 1 || number === 0) {
return number;
} else {
return number * factorial(number - 1);
}
}
console.log(factorial(3));
``````

Do you have any ideas on how recursion works?

Thank you!
Pummarin

Hey @pummarinbest!

One great way to see how recursion works inside your computer is to run your function through a debugger and go step by step. If you do that then you will get to see how the computer is executing your function.

For recursion we need a base case and a recursive case. The reason why we need a base case is because recursion is a function that calls itself. The base case stops the function from continuing to call itself or else it would just run forever.

Here is how your computer is executing this function.

``````// call the function
// check if statement  (does 3 = 1 or 3=0) No
// go to else statement (3 * fact(3-1))
//at this point the computer does not know the answer for
//3*fact(2) so it will call itself again. Now number = 2
//check if statement (does 2 =1 or 2=0) No
// go to else statement (2 * fact(2-1))
//at this point the computer does not know the answer for
//2*fact(1) so it will call itself again. Now number = 1
//check if statement (does 1 =1) yes
// we return 1
// now we work our way back up and
//plug in 1 for fact(1)
// 2*1 = 2 (answer for fact(2)
// 3 * fact(2) or 3*2 = 6
``````

You noticed that I didn’t go to the if statement and check if number === 0 because we already got our answer with number ===1. Really you don’t need that or operator in the if statement. You could just start with number ===1.

Hopefully that starts to make sense. I am also going to link some resources.

Happy coding!

The important thing to remember is that a function is a set of instructions, not a single entity.

If coding were cooking, a function would be a recipe, not an oven. Let’s say you’re making pretzels. You mix the ingredients and follow the instructions to roll, shape, boil, and salt a pretzel. Then the recipe says something like “make 12 more pretzels”. That’s the recipe referencing itself and that makes perfect sense in context. You can also have multiple people making pretzels at once, using the same recipe. It’s just following the steps in order.

The pretzel analogy doesn’t apply to recursion really, but I’ve found that where moat people get hung up on recursion is the fact that a function can be executed from many places at any time and the different executions don’t somehow collide woth each other.

1 Like

I like the pretzel analogy
Thanks!